/* replicaiton material for "Case selection and causal inferences in qualitative comparative research"
	Thomas Pluemper, Vera E. Troeger, and Eric Neumayer*/
	
	cd c:\vera\tp\casestudy\replication /*change working directory here once !!! */
	set more off
/*

Algorithms:

	1.	reasonable
		I.	max y - selection on DV
		II.	max x - most dissimilar
		III.	min z - most similar
		IV.	max y max x
			i.	max (dist y + dist x)
			ii.	max (dist y / max dist y + dist x / max dist x)
		V.	max y min z 
			i.	max (dist y - dist z)
			ii.	max (dist y / max dist y - dist z / max dist z)
		VI.	max x min z 
			i.	max (dist x - dist z) - difference PTN
			ii.	max (dist x / max dist x - dist z / max dist z) - weighted difference - PTN
		VII.	max y max x min z
			i.	max (dist y + dist x - dist z)
			ii.	max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	2.	suggested
			a	random y - KKV
			b	max z � Gerring / seawright: most different
			c	max (dist x / dist y) � Gerring / seawright: one variant of divers
			d	lijphart
			e	augmented lijphart - PTN
			f	max dist(x/y) - Gerring / seawrigth: one variant of divers


*/
	
********************************************************************************************
* A: experiments for continuous RHS variables
********************************************************************************************
* variation of no of obs; corr=0, SD(x1, z)=1
* 1. N=20
********************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 20
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/20 {

  		forvalues j=1/20 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'
	
	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 20
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)


* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)


	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2


* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)

	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

		
	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy


	end
	
	

	simulate maxy_N20=e(beta_maxy) maxx_N20=e(beta_maxx) minz_N20=e(beta_minz) sumyx_N20=e(beta_sumyx) wsumyx_N20=e(beta_wsumyx) diffyz_N20=e(beta_diffyz) wdiffyz_N20=e(beta_wdiffyz) diffxz_N20=e(beta_diffxz) wdiffxz_N20=e(beta_wdiffxz) yxz_N20=e(beta_yxz) wyxz_N20=e(beta_wyxz) random_N20=e(beta_random) maxz_N20=e(beta_maxz) ratioxy_N20=e(beta_ratioxy) lijp_N20=e(beta_lijp) lijp2_N20=e(beta_lijp2) maxxy_N20=e(beta_maxxy), reps(1000) saving(allcsd_N20.dta, replace every(10)): cds2

******************************************************************************************************************************************************************************************************************************************************************************
******************************************************************************************************************************************************************************************************************************************************************************
* 2. N=40
******************************************************************************************************************************************************************************************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 40
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/40 {

  		forvalues j=1/40 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 40
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_N40=e(beta_maxy) maxx_N40=e(beta_maxx) minz_N40=e(beta_minz) sumyx_N40=e(beta_sumyx) wsumyx_N40=e(beta_wsumyx) diffyz_N40=e(beta_diffyz) wdiffyz_N40=e(beta_wdiffyz) diffxz_N40=e(beta_diffxz) wdiffxz_N40=e(beta_wdiffxz) yxz_N40=e(beta_yxz) wyxz_N40=e(beta_wyxz) random_N40=e(beta_random) maxz_N40=e(beta_maxz) ratioxy_N40=e(beta_ratioxy) lijp_N40=e(beta_lijp) lijp2_N40=e(beta_lijp2) maxxy_N40=e(beta_maxxy), reps(1000) saving(allcsd_N40.dta, replace every(10)): cds2





****************************************************************************************************
****************************************************************************************************
* 3. N=60
****************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 60
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/60 {

  		forvalues j=1/60 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 60
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_N60=e(beta_maxy) maxx_N60=e(beta_maxx) minz_N60=e(beta_minz) sumyx_N60=e(beta_sumyx) wsumyx_N60=e(beta_wsumyx) diffyz_N60=e(beta_diffyz) wdiffyz_N60=e(beta_wdiffyz) diffxz_N60=e(beta_diffxz) wdiffxz_N60=e(beta_wdiffxz) yxz_N60=e(beta_yxz) wyxz_N60=e(beta_wyxz) random_N60=e(beta_random) maxz_N60=e(beta_maxz) ratioxy_N60=e(beta_ratioxy) lijp_N60=e(beta_lijp) lijp2_N60=e(beta_lijp2) maxxy_N60=e(beta_maxxy), reps(1000) saving(allcsd_N60.dta, replace every(10)): cds2





****************************************************************************************************
****************************************************************************************************
* 4. N=80
****************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 80
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/80 {

  		forvalues j=1/80 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 80
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_N80=e(beta_maxy) maxx_N80=e(beta_maxx) minz_N80=e(beta_minz) sumyx_N80=e(beta_sumyx) wsumyx_N80=e(beta_wsumyx) diffyz_N80=e(beta_diffyz) wdiffyz_N80=e(beta_wdiffyz) diffxz_N80=e(beta_diffxz) wdiffxz_N80=e(beta_wdiffxz) yxz_N80=e(beta_yxz) wyxz_N80=e(beta_wyxz) random_N80=e(beta_random) maxz_N80=e(beta_maxz) ratioxy_N80=e(beta_ratioxy) lijp_N80=e(beta_lijp) lijp2_N80=e(beta_lijp2) maxxy_N80=e(beta_maxxy), reps(1000) saving(allcsd_N80.dta, replace every(10)): cds2




****************************************************************************************************
****************************************************************************************************
* 5. N=100
****************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_N100=e(beta_maxy) maxx_N100=e(beta_maxx) minz_N100=e(beta_minz) sumyx_N100=e(beta_sumyx) wsumyx_N100=e(beta_wsumyx) diffyz_N100=e(beta_diffyz) wdiffyz_N100=e(beta_wdiffyz) diffxz_N100=e(beta_diffxz) wdiffxz_N100=e(beta_wdiffxz) yxz_N100=e(beta_yxz) wyxz_N100=e(beta_wyxz) random_N100=e(beta_random) maxz_N100=e(beta_maxz) ratioxy_N100=e(beta_ratioxy) lijp_N100=e(beta_lijp) lijp2_N100=e(beta_lijp2) maxxy_N100=e(beta_maxxy), reps(1000) saving(allcsd_N100.dta, replace every(10)): cds2



******************************************************************************************************************


******************************************************************************************************************
******************************************************************************************************************
* change correlation x1, z, N=100, SD(x1)=0.3
******************************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.9
***********************************************************************************************************
	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.9\-0.9,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn09=e(beta_maxy) maxx_sd03cn09=e(beta_maxx) minz_sd03cn09=e(beta_minz) sumyx_sd03cn09=e(beta_sumyx) wsumyx_sd03cn09=e(beta_wsumyx) diffyz_sd03cn09=e(beta_diffyz) wdiffyz_sd03cn09=e(beta_wdiffyz) diffxz_sd03cn09=e(beta_diffxz) wdiffxz_sd03cn09=e(beta_wdiffxz) yxz_sd03cn09=e(beta_yxz) wyxz_sd03cn09=e(beta_wyxz) random_sd03cn09=e(beta_random) maxz_sd03cn09=e(beta_maxz) ratioxy_sd03cn09=e(beta_ratioxy) lijp_sd03cn09=e(beta_lijp) lijp2_sd03cn09=e(beta_lijp2) maxxy_sd03cn09=e(beta_maxxy), reps(1000) saving(allcsd_sd03cn09.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.7\-0.7,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn07=e(beta_maxy) maxx_sd03cn07=e(beta_maxx) minz_sd03cn07=e(beta_minz) sumyx_sd03cn07=e(beta_sumyx) wsumyx_sd03cn07=e(beta_wsumyx) diffyz_sd03cn07=e(beta_diffyz) wdiffyz_sd03cn07=e(beta_wdiffyz) diffxz_sd03cn07=e(beta_diffxz) wdiffxz_sd03cn07=e(beta_wdiffxz) yxz_sd03cn07=e(beta_yxz) wyxz_sd03cn07=e(beta_wyxz) random_sd03cn07=e(beta_random) maxz_sd03cn07=e(beta_maxz) ratioxy_sd03cn07=e(beta_ratioxy) lijp_sd03cn07=e(beta_lijp) lijp2_sd03cn07=e(beta_lijp2) maxxy_sd03cn07=e(beta_maxxy), reps(1000) saving(allcsd_sd03cn07.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.5
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.5\-0.5,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn05=e(beta_maxy) maxx_sd03cn05=e(beta_maxx) minz_sd03cn05=e(beta_minz) sumyx_sd03cn05=e(beta_sumyx) wsumyx_sd03cn05=e(beta_wsumyx) diffyz_sd03cn05=e(beta_diffyz) wdiffyz_sd03cn05=e(beta_wdiffyz) diffxz_sd03cn05=e(beta_diffxz) wdiffxz_sd03cn05=e(beta_wdiffxz) yxz_sd03cn05=e(beta_yxz) wyxz_sd03cn05=e(beta_wyxz) random_sd03cn05=e(beta_random) maxz_sd03cn05=e(beta_maxz) ratioxy_sd03cn05=e(beta_ratioxy) lijp_sd03cn05=e(beta_lijp) lijp2_sd03cn05=e(beta_lijp2) maxxy_sd03cn05=e(beta_maxxy), reps(1000) saving(allcsd_sd03cn05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.3
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.3\-0.3,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn03=e(beta_maxy) maxx_sd03cn03=e(beta_maxx) minz_sd03cn03=e(beta_minz) sumyx_sd03cn03=e(beta_sumyx) wsumyx_sd03cn03=e(beta_wsumyx) diffyz_sd03cn03=e(beta_diffyz) wdiffyz_sd03cn03=e(beta_wdiffyz) diffxz_sd03cn03=e(beta_diffxz) wdiffxz_sd03cn03=e(beta_wdiffxz) yxz_sd03cn03=e(beta_yxz) wyxz_sd03cn03=e(beta_wyxz) random_sd03cn03=e(beta_random) maxz_sd03cn03=e(beta_maxz) ratioxy_sd03cn03=e(beta_ratioxy) lijp_sd03cn03=e(beta_lijp) lijp2_sd03cn03=e(beta_lijp2) maxxy_sd03cn03=e(beta_maxxy), reps(1000) saving(allcsd_sd03cn03.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.1\-0.1,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn01=e(beta_maxy) maxx_sd03cn01=e(beta_maxx) minz_sd03cn01=e(beta_minz) sumyx_sd03cn01=e(beta_sumyx) wsumyx_sd03cn01=e(beta_wsumyx) diffyz_sd03cn01=e(beta_diffyz) wdiffyz_sd03cn01=e(beta_wdiffyz) diffxz_sd03cn01=e(beta_diffxz) wdiffxz_sd03cn01=e(beta_wdiffxz) yxz_sd03cn01=e(beta_yxz) wyxz_sd03cn01=e(beta_wyxz) random_sd03cn01=e(beta_random) maxz_sd03cn01=e(beta_maxz) ratioxy_sd03cn01=e(beta_ratioxy) lijp_sd03cn01=e(beta_lijp) lijp2_sd03cn01=e(beta_lijp2) maxxy_sd03cn01=e(beta_maxxy), reps(1000) saving(allcsd_sd03cn01.dta, replace every(10)): cds2





******************************************************************************************************************
******************************************************************************************************************
* corr(x1, z)=0
******************************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn00=e(beta_maxy) maxx_sd03cn00=e(beta_maxx) minz_sd03cn00=e(beta_minz) sumyx_sd03cn00=e(beta_sumyx) wsumyx_sd03cn00=e(beta_wsumyx) diffyz_sd03cn00=e(beta_diffyz) wdiffyz_sd03cn00=e(beta_wdiffyz) diffxz_sd03cn00=e(beta_diffxz) wdiffxz_sd03cn00=e(beta_wdiffxz) yxz_sd03cn00=e(beta_yxz) wyxz_sd03cn00=e(beta_wyxz) random_sd03cn00=e(beta_random) maxz_sd03cn00=e(beta_maxz) ratioxy_sd03cn00=e(beta_ratioxy) lijp_sd03cn00=e(beta_lijp) lijp2_sd03cn00=e(beta_lijp2) maxxy_sd03cn00=e(beta_maxxy), reps(1000) saving(allcsd_sd03cn00.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.1\0.1,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03c01=e(beta_maxy) maxx_sd03c01=e(beta_maxx) minz_sd03c01=e(beta_minz) sumyx_sd03c01=e(beta_sumyx) wsumyx_sd03c01=e(beta_wsumyx) diffyz_sd03c01=e(beta_diffyz) wdiffyz_sd03c01=e(beta_wdiffyz) diffxz_sd03c01=e(beta_diffxz) wdiffxz_sd03c01=e(beta_wdiffxz) yxz_sd03c01=e(beta_yxz) wyxz_sd03c01=e(beta_wyxz) random_sd03c01=e(beta_random) maxz_sd03c01=e(beta_maxz) ratioxy_sd03c01=e(beta_ratioxy) lijp_sd03c01=e(beta_lijp) lijp2_sd03c01=e(beta_lijp2) maxxy_sd03c01=e(beta_maxxy), reps(1000) saving(allcsd_sd03c01.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.3
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.3\0.3,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03c03=e(beta_maxy) maxx_sd03c03=e(beta_maxx) minz_sd03c03=e(beta_minz) sumyx_sd03c03=e(beta_sumyx) wsumyx_sd03c03=e(beta_wsumyx) diffyz_sd03c03=e(beta_diffyz) wdiffyz_sd03c03=e(beta_wdiffyz) diffxz_sd03c03=e(beta_diffxz) wdiffxz_sd03c03=e(beta_wdiffxz) yxz_sd03c03=e(beta_yxz) wyxz_sd03c03=e(beta_wyxz) random_sd03c03=e(beta_random) maxz_sd03c03=e(beta_maxz) ratioxy_sd03c03=e(beta_ratioxy) lijp_sd03c03=e(beta_lijp) lijp2_sd03c03=e(beta_lijp2) maxxy_sd03c03=e(beta_maxxy), reps(1000) saving(allcsd_sd03c03.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.5
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.5\0.5,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03c05=e(beta_maxy) maxx_sd03c05=e(beta_maxx) minz_sd03c05=e(beta_minz) sumyx_sd03c05=e(beta_sumyx) wsumyx_sd03c05=e(beta_wsumyx) diffyz_sd03c05=e(beta_diffyz) wdiffyz_sd03c05=e(beta_wdiffyz) diffxz_sd03c05=e(beta_diffxz) wdiffxz_sd03c05=e(beta_wdiffxz) yxz_sd03c05=e(beta_yxz) wyxz_sd03c05=e(beta_wyxz) random_sd03c05=e(beta_random) maxz_sd03c05=e(beta_maxz) ratioxy_sd03c05=e(beta_ratioxy) lijp_sd03c05=e(beta_lijp) lijp2_sd03c05=e(beta_lijp2) maxxy_sd03c05=e(beta_maxxy), reps(1000) saving(allcsd_sd03c05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.7\0.7,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03c07=e(beta_maxy) maxx_sd03c07=e(beta_maxx) minz_sd03c07=e(beta_minz) sumyx_sd03c07=e(beta_sumyx) wsumyx_sd03c07=e(beta_wsumyx) diffyz_sd03c07=e(beta_diffyz) wdiffyz_sd03c07=e(beta_wdiffyz) diffxz_sd03c07=e(beta_diffxz) wdiffxz_sd03c07=e(beta_wdiffxz) yxz_sd03c07=e(beta_yxz) wyxz_sd03c07=e(beta_wyxz) random_sd03c07=e(beta_random) maxz_sd03c07=e(beta_maxz) ratioxy_sd03c07=e(beta_ratioxy) lijp_sd03c07=e(beta_lijp) lijp2_sd03c07=e(beta_lijp2) maxxy_sd03c07=e(beta_maxxy), reps(1000) saving(allcsd_sd03c07.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.9
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.9\0.9,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03c09=e(beta_maxy) maxx_sd03c09=e(beta_maxx) minz_sd03c09=e(beta_minz) sumyx_sd03c09=e(beta_sumyx) wsumyx_sd03c09=e(beta_wsumyx) diffyz_sd03c09=e(beta_diffyz) wdiffyz_sd03c09=e(beta_wdiffyz) diffxz_sd03c09=e(beta_diffxz) wdiffxz_sd03c09=e(beta_wdiffxz) yxz_sd03c09=e(beta_yxz) wyxz_sd03c09=e(beta_wyxz) random_sd03c09=e(beta_random) maxz_sd03c09=e(beta_maxz) ratioxy_sd03c09=e(beta_ratioxy) lijp_sd03c09=e(beta_lijp) lijp2_sd03c09=e(beta_lijp2) maxxy_sd03c09=e(beta_maxxy), reps(1000) saving(allcsd_sd03c09.dta, replace every(10)): cds2




******************************************************************************************************************


******************************************************************************************************************
******************************************************************************************************************
* change correlation x1, z, N=100, SD(x1)=0.7
******************************************************************************************************************

***********************************************************************************************************
* corr(x1,z)= -0.9
***********************************************************************************************************
	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.9\-0.9,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn09=e(beta_maxy) maxx_sd07cn09=e(beta_maxx) minz_sd07cn09=e(beta_minz) sumyx_sd07cn09=e(beta_sumyx) wsumyx_sd07cn09=e(beta_wsumyx) diffyz_sd07cn09=e(beta_diffyz) wdiffyz_sd07cn09=e(beta_wdiffyz) diffxz_sd07cn09=e(beta_diffxz) wdiffxz_sd07cn09=e(beta_wdiffxz) yxz_sd07cn09=e(beta_yxz) wyxz_sd07cn09=e(beta_wyxz) random_sd07cn09=e(beta_random) maxz_sd07cn09=e(beta_maxz) ratioxy_sd07cn09=e(beta_ratioxy) lijp_sd07cn09=e(beta_lijp) lijp2_sd07cn09=e(beta_lijp2) maxxy_sd07cn09=e(beta_maxxy), reps(1000) saving(allcsd_sd07cn09.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.7\-0.7,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn07=e(beta_maxy) maxx_sd07cn07=e(beta_maxx) minz_sd07cn07=e(beta_minz) sumyx_sd07cn07=e(beta_sumyx) wsumyx_sd07cn07=e(beta_wsumyx) diffyz_sd07cn07=e(beta_diffyz) wdiffyz_sd07cn07=e(beta_wdiffyz) diffxz_sd07cn07=e(beta_diffxz) wdiffxz_sd07cn07=e(beta_wdiffxz) yxz_sd07cn07=e(beta_yxz) wyxz_sd07cn07=e(beta_wyxz) random_sd07cn07=e(beta_random) maxz_sd07cn07=e(beta_maxz) ratioxy_sd07cn07=e(beta_ratioxy) lijp_sd07cn07=e(beta_lijp) lijp2_sd07cn07=e(beta_lijp2) maxxy_sd07cn07=e(beta_maxxy), reps(1000) saving(allcsd_sd07cn07.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.5
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.5\-0.5,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn05=e(beta_maxy) maxx_sd07cn05=e(beta_maxx) minz_sd07cn05=e(beta_minz) sumyx_sd07cn05=e(beta_sumyx) wsumyx_sd07cn05=e(beta_wsumyx) diffyz_sd07cn05=e(beta_diffyz) wdiffyz_sd07cn05=e(beta_wdiffyz) diffxz_sd07cn05=e(beta_diffxz) wdiffxz_sd07cn05=e(beta_wdiffxz) yxz_sd07cn05=e(beta_yxz) wyxz_sd07cn05=e(beta_wyxz) random_sd07cn05=e(beta_random) maxz_sd07cn05=e(beta_maxz) ratioxy_sd07cn05=e(beta_ratioxy) lijp_sd07cn05=e(beta_lijp) lijp2_sd07cn05=e(beta_lijp2) maxxy_sd07cn05=e(beta_maxxy), reps(1000) saving(allcsd_sd07cn05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.3
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.3\-0.3,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn03=e(beta_maxy) maxx_sd07cn03=e(beta_maxx) minz_sd07cn03=e(beta_minz) sumyx_sd07cn03=e(beta_sumyx) wsumyx_sd07cn03=e(beta_wsumyx) diffyz_sd07cn03=e(beta_diffyz) wdiffyz_sd07cn03=e(beta_wdiffyz) diffxz_sd07cn03=e(beta_diffxz) wdiffxz_sd07cn03=e(beta_wdiffxz) yxz_sd07cn03=e(beta_yxz) wyxz_sd07cn03=e(beta_wyxz) random_sd07cn03=e(beta_random) maxz_sd07cn03=e(beta_maxz) ratioxy_sd07cn03=e(beta_ratioxy) lijp_sd07cn03=e(beta_lijp) lijp2_sd07cn03=e(beta_lijp2) maxxy_sd07cn03=e(beta_maxxy), reps(1000) saving(allcsd_sd07cn03.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.1\-0.1,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn01=e(beta_maxy) maxx_sd07cn01=e(beta_maxx) minz_sd07cn01=e(beta_minz) sumyx_sd07cn01=e(beta_sumyx) wsumyx_sd07cn01=e(beta_wsumyx) diffyz_sd07cn01=e(beta_diffyz) wdiffyz_sd07cn01=e(beta_wdiffyz) diffxz_sd07cn01=e(beta_diffxz) wdiffxz_sd07cn01=e(beta_wdiffxz) yxz_sd07cn01=e(beta_yxz) wyxz_sd07cn01=e(beta_wyxz) random_sd07cn01=e(beta_random) maxz_sd07cn01=e(beta_maxz) ratioxy_sd07cn01=e(beta_ratioxy) lijp_sd07cn01=e(beta_lijp) lijp2_sd07cn01=e(beta_lijp2) maxxy_sd07cn01=e(beta_maxxy), reps(1000) saving(allcsd_sd07cn01.dta, replace every(10)): cds2





******************************************************************************************************************
******************************************************************************************************************
* corr(x1, z)=0
******************************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn00=e(beta_maxy) maxx_sd07cn00=e(beta_maxx) minz_sd07cn00=e(beta_minz) sumyx_sd07cn00=e(beta_sumyx) wsumyx_sd07cn00=e(beta_wsumyx) diffyz_sd07cn00=e(beta_diffyz) wdiffyz_sd07cn00=e(beta_wdiffyz) diffxz_sd07cn00=e(beta_diffxz) wdiffxz_sd07cn00=e(beta_wdiffxz) yxz_sd07cn00=e(beta_yxz) wyxz_sd07cn00=e(beta_wyxz) random_sd07cn00=e(beta_random) maxz_sd07cn00=e(beta_maxz) ratioxy_sd07cn00=e(beta_ratioxy) lijp_sd07cn00=e(beta_lijp) lijp2_sd07cn00=e(beta_lijp2) maxxy_sd07cn00=e(beta_maxxy), reps(1000) saving(allcsd_sd07cn00.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.1\0.1,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07c01=e(beta_maxy) maxx_sd07c01=e(beta_maxx) minz_sd07c01=e(beta_minz) sumyx_sd07c01=e(beta_sumyx) wsumyx_sd07c01=e(beta_wsumyx) diffyz_sd07c01=e(beta_diffyz) wdiffyz_sd07c01=e(beta_wdiffyz) diffxz_sd07c01=e(beta_diffxz) wdiffxz_sd07c01=e(beta_wdiffxz) yxz_sd07c01=e(beta_yxz) wyxz_sd07c01=e(beta_wyxz) random_sd07c01=e(beta_random) maxz_sd07c01=e(beta_maxz) ratioxy_sd07c01=e(beta_ratioxy) lijp_sd07c01=e(beta_lijp) lijp2_sd07c01=e(beta_lijp2) maxxy_sd07c01=e(beta_maxxy), reps(1000) saving(allcsd_sd07c01.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.3
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.3\0.3,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07c03=e(beta_maxy) maxx_sd07c03=e(beta_maxx) minz_sd07c03=e(beta_minz) sumyx_sd07c03=e(beta_sumyx) wsumyx_sd07c03=e(beta_wsumyx) diffyz_sd07c03=e(beta_diffyz) wdiffyz_sd07c03=e(beta_wdiffyz) diffxz_sd07c03=e(beta_diffxz) wdiffxz_sd07c03=e(beta_wdiffxz) yxz_sd07c03=e(beta_yxz) wyxz_sd07c03=e(beta_wyxz) random_sd07c03=e(beta_random) maxz_sd07c03=e(beta_maxz) ratioxy_sd07c03=e(beta_ratioxy) lijp_sd07c03=e(beta_lijp) lijp2_sd07c03=e(beta_lijp2) maxxy_sd07c03=e(beta_maxxy), reps(1000) saving(allcsd_sd07c03.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.5
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.5\0.5,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07c05=e(beta_maxy) maxx_sd07c05=e(beta_maxx) minz_sd07c05=e(beta_minz) sumyx_sd07c05=e(beta_sumyx) wsumyx_sd07c05=e(beta_wsumyx) diffyz_sd07c05=e(beta_diffyz) wdiffyz_sd07c05=e(beta_wdiffyz) diffxz_sd07c05=e(beta_diffxz) wdiffxz_sd07c05=e(beta_wdiffxz) yxz_sd07c05=e(beta_yxz) wyxz_sd07c05=e(beta_wyxz) random_sd07c05=e(beta_random) maxz_sd07c05=e(beta_maxz) ratioxy_sd07c05=e(beta_ratioxy) lijp_sd07c05=e(beta_lijp) lijp2_sd07c05=e(beta_lijp2) maxxy_sd07c05=e(beta_maxxy), reps(1000) saving(allcsd_sd07c05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.7\0.7,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07c07=e(beta_maxy) maxx_sd07c07=e(beta_maxx) minz_sd07c07=e(beta_minz) sumyx_sd07c07=e(beta_sumyx) wsumyx_sd07c07=e(beta_wsumyx) diffyz_sd07c07=e(beta_diffyz) wdiffyz_sd07c07=e(beta_wdiffyz) diffxz_sd07c07=e(beta_diffxz) wdiffxz_sd07c07=e(beta_wdiffxz) yxz_sd07c07=e(beta_yxz) wyxz_sd07c07=e(beta_wyxz) random_sd07c07=e(beta_random) maxz_sd07c07=e(beta_maxz) ratioxy_sd07c07=e(beta_ratioxy) lijp_sd07c07=e(beta_lijp) lijp2_sd07c07=e(beta_lijp2) maxxy_sd07c07=e(beta_maxxy), reps(1000) saving(allcsd_sd07c07.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.9
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.9\0.9,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07c09=e(beta_maxy) maxx_sd07c09=e(beta_maxx) minz_sd07c09=e(beta_minz) sumyx_sd07c09=e(beta_sumyx) wsumyx_sd07c09=e(beta_wsumyx) diffyz_sd07c09=e(beta_diffyz) wdiffyz_sd07c09=e(beta_wdiffyz) diffxz_sd07c09=e(beta_diffxz) wdiffxz_sd07c09=e(beta_wdiffxz) yxz_sd07c09=e(beta_yxz) wyxz_sd07c09=e(beta_wyxz) random_sd07c09=e(beta_random) maxz_sd07c09=e(beta_maxz) ratioxy_sd07c09=e(beta_ratioxy) lijp_sd07c09=e(beta_lijp) lijp2_sd07c09=e(beta_lijp2) maxxy_sd07c09=e(beta_maxxy), reps(1000) saving(allcsd_sd07c09.dta, replace every(10)): cds2




******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************
* change correlation x1, z, N=100, SD(x1)=1
******************************************************************************************************************

***********************************************************************************************************
* corr(x1,z)= -0.9
***********************************************************************************************************
	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.9\-0.9,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn09=e(beta_maxy) maxx_sd10cn09=e(beta_maxx) minz_sd10cn09=e(beta_minz) sumyx_sd10cn09=e(beta_sumyx) wsumyx_sd10cn09=e(beta_wsumyx) diffyz_sd10cn09=e(beta_diffyz) wdiffyz_sd10cn09=e(beta_wdiffyz) diffxz_sd10cn09=e(beta_diffxz) wdiffxz_sd10cn09=e(beta_wdiffxz) yxz_sd10cn09=e(beta_yxz) wyxz_sd10cn09=e(beta_wyxz) random_sd10cn09=e(beta_random) maxz_sd10cn09=e(beta_maxz) ratioxy_sd10cn09=e(beta_ratioxy) lijp_sd10cn09=e(beta_lijp) lijp2_sd10cn09=e(beta_lijp2) maxxy_sd10cn09=e(beta_maxxy), reps(1000) saving(allcsd_sd10cn09.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.7\-0.7,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn07=e(beta_maxy) maxx_sd10cn07=e(beta_maxx) minz_sd10cn07=e(beta_minz) sumyx_sd10cn07=e(beta_sumyx) wsumyx_sd10cn07=e(beta_wsumyx) diffyz_sd10cn07=e(beta_diffyz) wdiffyz_sd10cn07=e(beta_wdiffyz) diffxz_sd10cn07=e(beta_diffxz) wdiffxz_sd10cn07=e(beta_wdiffxz) yxz_sd10cn07=e(beta_yxz) wyxz_sd10cn07=e(beta_wyxz) random_sd10cn07=e(beta_random) maxz_sd10cn07=e(beta_maxz) ratioxy_sd10cn07=e(beta_ratioxy) lijp_sd10cn07=e(beta_lijp) lijp2_sd10cn07=e(beta_lijp2) maxxy_sd10cn07=e(beta_maxxy), reps(1000) saving(allcsd_sd10cn07.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.5
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.5\-0.5,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn05=e(beta_maxy) maxx_sd10cn05=e(beta_maxx) minz_sd10cn05=e(beta_minz) sumyx_sd10cn05=e(beta_sumyx) wsumyx_sd10cn05=e(beta_wsumyx) diffyz_sd10cn05=e(beta_diffyz) wdiffyz_sd10cn05=e(beta_wdiffyz) diffxz_sd10cn05=e(beta_diffxz) wdiffxz_sd10cn05=e(beta_wdiffxz) yxz_sd10cn05=e(beta_yxz) wyxz_sd10cn05=e(beta_wyxz) random_sd10cn05=e(beta_random) maxz_sd10cn05=e(beta_maxz) ratioxy_sd10cn05=e(beta_ratioxy) lijp_sd10cn05=e(beta_lijp) lijp2_sd10cn05=e(beta_lijp2) maxxy_sd10cn05=e(beta_maxxy), reps(1000) saving(allcsd_sd10cn05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.3
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.3\-0.3,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn03=e(beta_maxy) maxx_sd10cn03=e(beta_maxx) minz_sd10cn03=e(beta_minz) sumyx_sd10cn03=e(beta_sumyx) wsumyx_sd10cn03=e(beta_wsumyx) diffyz_sd10cn03=e(beta_diffyz) wdiffyz_sd10cn03=e(beta_wdiffyz) diffxz_sd10cn03=e(beta_diffxz) wdiffxz_sd10cn03=e(beta_wdiffxz) yxz_sd10cn03=e(beta_yxz) wyxz_sd10cn03=e(beta_wyxz) random_sd10cn03=e(beta_random) maxz_sd10cn03=e(beta_maxz) ratioxy_sd10cn03=e(beta_ratioxy) lijp_sd10cn03=e(beta_lijp) lijp2_sd10cn03=e(beta_lijp2) maxxy_sd10cn03=e(beta_maxxy), reps(1000) saving(allcsd_sd10cn03.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.1\-0.1,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn01=e(beta_maxy) maxx_sd10cn01=e(beta_maxx) minz_sd10cn01=e(beta_minz) sumyx_sd10cn01=e(beta_sumyx) wsumyx_sd10cn01=e(beta_wsumyx) diffyz_sd10cn01=e(beta_diffyz) wdiffyz_sd10cn01=e(beta_wdiffyz) diffxz_sd10cn01=e(beta_diffxz) wdiffxz_sd10cn01=e(beta_wdiffxz) yxz_sd10cn01=e(beta_yxz) wyxz_sd10cn01=e(beta_wyxz) random_sd10cn01=e(beta_random) maxz_sd10cn01=e(beta_maxz) ratioxy_sd10cn01=e(beta_ratioxy) lijp_sd10cn01=e(beta_lijp) lijp2_sd10cn01=e(beta_lijp2) maxxy_sd10cn01=e(beta_maxxy), reps(1000) saving(allcsd_sd10cn01.dta, replace every(10)): cds2





******************************************************************************************************************
******************************************************************************************************************
* corr(x1, z)=0
******************************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn00=e(beta_maxy) maxx_sd10cn00=e(beta_maxx) minz_sd10cn00=e(beta_minz) sumyx_sd10cn00=e(beta_sumyx) wsumyx_sd10cn00=e(beta_wsumyx) diffyz_sd10cn00=e(beta_diffyz) wdiffyz_sd10cn00=e(beta_wdiffyz) diffxz_sd10cn00=e(beta_diffxz) wdiffxz_sd10cn00=e(beta_wdiffxz) yxz_sd10cn00=e(beta_yxz) wyxz_sd10cn00=e(beta_wyxz) random_sd10cn00=e(beta_random) maxz_sd10cn00=e(beta_maxz) ratioxy_sd10cn00=e(beta_ratioxy) lijp_sd10cn00=e(beta_lijp) lijp2_sd10cn00=e(beta_lijp2) maxxy_sd10cn00=e(beta_maxxy), reps(1000) saving(allcsd_sd10cn00.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.1\0.1,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10c01=e(beta_maxy) maxx_sd10c01=e(beta_maxx) minz_sd10c01=e(beta_minz) sumyx_sd10c01=e(beta_sumyx) wsumyx_sd10c01=e(beta_wsumyx) diffyz_sd10c01=e(beta_diffyz) wdiffyz_sd10c01=e(beta_wdiffyz) diffxz_sd10c01=e(beta_diffxz) wdiffxz_sd10c01=e(beta_wdiffxz) yxz_sd10c01=e(beta_yxz) wyxz_sd10c01=e(beta_wyxz) random_sd10c01=e(beta_random) maxz_sd10c01=e(beta_maxz) ratioxy_sd10c01=e(beta_ratioxy) lijp_sd10c01=e(beta_lijp) lijp2_sd10c01=e(beta_lijp2) maxxy_sd10c01=e(beta_maxxy), reps(1000) saving(allcsd_sd10c01.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.3
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.3\0.3,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10c03=e(beta_maxy) maxx_sd10c03=e(beta_maxx) minz_sd10c03=e(beta_minz) sumyx_sd10c03=e(beta_sumyx) wsumyx_sd10c03=e(beta_wsumyx) diffyz_sd10c03=e(beta_diffyz) wdiffyz_sd10c03=e(beta_wdiffyz) diffxz_sd10c03=e(beta_diffxz) wdiffxz_sd10c03=e(beta_wdiffxz) yxz_sd10c03=e(beta_yxz) wyxz_sd10c03=e(beta_wyxz) random_sd10c03=e(beta_random) maxz_sd10c03=e(beta_maxz) ratioxy_sd10c03=e(beta_ratioxy) lijp_sd10c03=e(beta_lijp) lijp2_sd10c03=e(beta_lijp2) maxxy_sd10c03=e(beta_maxxy), reps(1000) saving(allcsd_sd10c03.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.5
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.5\0.5,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10c05=e(beta_maxy) maxx_sd10c05=e(beta_maxx) minz_sd10c05=e(beta_minz) sumyx_sd10c05=e(beta_sumyx) wsumyx_sd10c05=e(beta_wsumyx) diffyz_sd10c05=e(beta_diffyz) wdiffyz_sd10c05=e(beta_wdiffyz) diffxz_sd10c05=e(beta_diffxz) wdiffxz_sd10c05=e(beta_wdiffxz) yxz_sd10c05=e(beta_yxz) wyxz_sd10c05=e(beta_wyxz) random_sd10c05=e(beta_random) maxz_sd10c05=e(beta_maxz) ratioxy_sd10c05=e(beta_ratioxy) lijp_sd10c05=e(beta_lijp) lijp2_sd10c05=e(beta_lijp2) maxxy_sd10c05=e(beta_maxxy), reps(1000) saving(allcsd_sd10c05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.7\0.7,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10c07=e(beta_maxy) maxx_sd10c07=e(beta_maxx) minz_sd10c07=e(beta_minz) sumyx_sd10c07=e(beta_sumyx) wsumyx_sd10c07=e(beta_wsumyx) diffyz_sd10c07=e(beta_diffyz) wdiffyz_sd10c07=e(beta_wdiffyz) diffxz_sd10c07=e(beta_diffxz) wdiffxz_sd10c07=e(beta_wdiffxz) yxz_sd10c07=e(beta_yxz) wyxz_sd10c07=e(beta_wyxz) random_sd10c07=e(beta_random) maxz_sd10c07=e(beta_maxz) ratioxy_sd10c07=e(beta_ratioxy) lijp_sd10c07=e(beta_lijp) lijp2_sd10c07=e(beta_lijp2) maxxy_sd10c07=e(beta_maxxy), reps(1000) saving(allcsd_sd10c07.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.9
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.9\0.9,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10c09=e(beta_maxy) maxx_sd10c09=e(beta_maxx) minz_sd10c09=e(beta_minz) sumyx_sd10c09=e(beta_sumyx) wsumyx_sd10c09=e(beta_wsumyx) diffyz_sd10c09=e(beta_diffyz) wdiffyz_sd10c09=e(beta_wdiffyz) diffxz_sd10c09=e(beta_diffxz) wdiffxz_sd10c09=e(beta_wdiffxz) yxz_sd10c09=e(beta_yxz) wyxz_sd10c09=e(beta_wyxz) random_sd10c09=e(beta_random) maxz_sd10c09=e(beta_maxz) ratioxy_sd10c09=e(beta_ratioxy) lijp_sd10c09=e(beta_lijp) lijp2_sd10c09=e(beta_lijp2) maxxy_sd10c09=e(beta_maxxy), reps(1000) saving(allcsd_sd10c09.dta, replace every(10)): cds2




******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************
* change correlation x1, z, N=100, SD(x1)=1.5
******************************************************************************************************************

***********************************************************************************************************
* corr(x1,z)= -0.9
***********************************************************************************************************
	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.9\-0.9,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn09=e(beta_maxy) maxx_sd15cn09=e(beta_maxx) minz_sd15cn09=e(beta_minz) sumyx_sd15cn09=e(beta_sumyx) wsumyx_sd15cn09=e(beta_wsumyx) diffyz_sd15cn09=e(beta_diffyz) wdiffyz_sd15cn09=e(beta_wdiffyz) diffxz_sd15cn09=e(beta_diffxz) wdiffxz_sd15cn09=e(beta_wdiffxz) yxz_sd15cn09=e(beta_yxz) wyxz_sd15cn09=e(beta_wyxz) random_sd15cn09=e(beta_random) maxz_sd15cn09=e(beta_maxz) ratioxy_sd15cn09=e(beta_ratioxy) lijp_sd15cn09=e(beta_lijp) lijp2_sd15cn09=e(beta_lijp2) maxxy_sd15cn09=e(beta_maxxy), reps(1000) saving(allcsd_sd15cn09.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.7\-0.7,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn07=e(beta_maxy) maxx_sd15cn07=e(beta_maxx) minz_sd15cn07=e(beta_minz) sumyx_sd15cn07=e(beta_sumyx) wsumyx_sd15cn07=e(beta_wsumyx) diffyz_sd15cn07=e(beta_diffyz) wdiffyz_sd15cn07=e(beta_wdiffyz) diffxz_sd15cn07=e(beta_diffxz) wdiffxz_sd15cn07=e(beta_wdiffxz) yxz_sd15cn07=e(beta_yxz) wyxz_sd15cn07=e(beta_wyxz) random_sd15cn07=e(beta_random) maxz_sd15cn07=e(beta_maxz) ratioxy_sd15cn07=e(beta_ratioxy) lijp_sd15cn07=e(beta_lijp) lijp2_sd15cn07=e(beta_lijp2) maxxy_sd15cn07=e(beta_maxxy), reps(1000) saving(allcsd_sd15cn07.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.5
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.5\-0.5,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn05=e(beta_maxy) maxx_sd15cn05=e(beta_maxx) minz_sd15cn05=e(beta_minz) sumyx_sd15cn05=e(beta_sumyx) wsumyx_sd15cn05=e(beta_wsumyx) diffyz_sd15cn05=e(beta_diffyz) wdiffyz_sd15cn05=e(beta_wdiffyz) diffxz_sd15cn05=e(beta_diffxz) wdiffxz_sd15cn05=e(beta_wdiffxz) yxz_sd15cn05=e(beta_yxz) wyxz_sd15cn05=e(beta_wyxz) random_sd15cn05=e(beta_random) maxz_sd15cn05=e(beta_maxz) ratioxy_sd15cn05=e(beta_ratioxy) lijp_sd15cn05=e(beta_lijp) lijp2_sd15cn05=e(beta_lijp2) maxxy_sd15cn05=e(beta_maxxy), reps(1000) saving(allcsd_sd15cn05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.3
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.3\-0.3,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn03=e(beta_maxy) maxx_sd15cn03=e(beta_maxx) minz_sd15cn03=e(beta_minz) sumyx_sd15cn03=e(beta_sumyx) wsumyx_sd15cn03=e(beta_wsumyx) diffyz_sd15cn03=e(beta_diffyz) wdiffyz_sd15cn03=e(beta_wdiffyz) diffxz_sd15cn03=e(beta_diffxz) wdiffxz_sd15cn03=e(beta_wdiffxz) yxz_sd15cn03=e(beta_yxz) wyxz_sd15cn03=e(beta_wyxz) random_sd15cn03=e(beta_random) maxz_sd15cn03=e(beta_maxz) ratioxy_sd15cn03=e(beta_ratioxy) lijp_sd15cn03=e(beta_lijp) lijp2_sd15cn03=e(beta_lijp2) maxxy_sd15cn03=e(beta_maxxy), reps(1000) saving(allcsd_sd15cn03.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.1\-0.1,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn01=e(beta_maxy) maxx_sd15cn01=e(beta_maxx) minz_sd15cn01=e(beta_minz) sumyx_sd15cn01=e(beta_sumyx) wsumyx_sd15cn01=e(beta_wsumyx) diffyz_sd15cn01=e(beta_diffyz) wdiffyz_sd15cn01=e(beta_wdiffyz) diffxz_sd15cn01=e(beta_diffxz) wdiffxz_sd15cn01=e(beta_wdiffxz) yxz_sd15cn01=e(beta_yxz) wyxz_sd15cn01=e(beta_wyxz) random_sd15cn01=e(beta_random) maxz_sd15cn01=e(beta_maxz) ratioxy_sd15cn01=e(beta_ratioxy) lijp_sd15cn01=e(beta_lijp) lijp2_sd15cn01=e(beta_lijp2) maxxy_sd15cn01=e(beta_maxxy), reps(1000) saving(allcsd_sd15cn01.dta, replace every(10)): cds2





******************************************************************************************************************
******************************************************************************************************************
* corr(x1, z)=0
******************************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn00=e(beta_maxy) maxx_sd15cn00=e(beta_maxx) minz_sd15cn00=e(beta_minz) sumyx_sd15cn00=e(beta_sumyx) wsumyx_sd15cn00=e(beta_wsumyx) diffyz_sd15cn00=e(beta_diffyz) wdiffyz_sd15cn00=e(beta_wdiffyz) diffxz_sd15cn00=e(beta_diffxz) wdiffxz_sd15cn00=e(beta_wdiffxz) yxz_sd15cn00=e(beta_yxz) wyxz_sd15cn00=e(beta_wyxz) random_sd15cn00=e(beta_random) maxz_sd15cn00=e(beta_maxz) ratioxy_sd15cn00=e(beta_ratioxy) lijp_sd15cn00=e(beta_lijp) lijp2_sd15cn00=e(beta_lijp2) maxxy_sd15cn00=e(beta_maxxy), reps(1000) saving(allcsd_sd15cn00.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.1\0.1,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15c01=e(beta_maxy) maxx_sd15c01=e(beta_maxx) minz_sd15c01=e(beta_minz) sumyx_sd15c01=e(beta_sumyx) wsumyx_sd15c01=e(beta_wsumyx) diffyz_sd15c01=e(beta_diffyz) wdiffyz_sd15c01=e(beta_wdiffyz) diffxz_sd15c01=e(beta_diffxz) wdiffxz_sd15c01=e(beta_wdiffxz) yxz_sd15c01=e(beta_yxz) wyxz_sd15c01=e(beta_wyxz) random_sd15c01=e(beta_random) maxz_sd15c01=e(beta_maxz) ratioxy_sd15c01=e(beta_ratioxy) lijp_sd15c01=e(beta_lijp) lijp2_sd15c01=e(beta_lijp2) maxxy_sd15c01=e(beta_maxxy), reps(1000) saving(allcsd_sd15c01.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.3
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.3\0.3,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15c03=e(beta_maxy) maxx_sd15c03=e(beta_maxx) minz_sd15c03=e(beta_minz) sumyx_sd15c03=e(beta_sumyx) wsumyx_sd15c03=e(beta_wsumyx) diffyz_sd15c03=e(beta_diffyz) wdiffyz_sd15c03=e(beta_wdiffyz) diffxz_sd15c03=e(beta_diffxz) wdiffxz_sd15c03=e(beta_wdiffxz) yxz_sd15c03=e(beta_yxz) wyxz_sd15c03=e(beta_wyxz) random_sd15c03=e(beta_random) maxz_sd15c03=e(beta_maxz) ratioxy_sd15c03=e(beta_ratioxy) lijp_sd15c03=e(beta_lijp) lijp2_sd15c03=e(beta_lijp2) maxxy_sd15c03=e(beta_maxxy), reps(1000) saving(allcsd_sd15c03.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.5
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.5\0.5,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15c05=e(beta_maxy) maxx_sd15c05=e(beta_maxx) minz_sd15c05=e(beta_minz) sumyx_sd15c05=e(beta_sumyx) wsumyx_sd15c05=e(beta_wsumyx) diffyz_sd15c05=e(beta_diffyz) wdiffyz_sd15c05=e(beta_wdiffyz) diffxz_sd15c05=e(beta_diffxz) wdiffxz_sd15c05=e(beta_wdiffxz) yxz_sd15c05=e(beta_yxz) wyxz_sd15c05=e(beta_wyxz) random_sd15c05=e(beta_random) maxz_sd15c05=e(beta_maxz) ratioxy_sd15c05=e(beta_ratioxy) lijp_sd15c05=e(beta_lijp) lijp2_sd15c05=e(beta_lijp2) maxxy_sd15c05=e(beta_maxxy), reps(1000) saving(allcsd_sd15c05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.7\0.7,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15c07=e(beta_maxy) maxx_sd15c07=e(beta_maxx) minz_sd15c07=e(beta_minz) sumyx_sd15c07=e(beta_sumyx) wsumyx_sd15c07=e(beta_wsumyx) diffyz_sd15c07=e(beta_diffyz) wdiffyz_sd15c07=e(beta_wdiffyz) diffxz_sd15c07=e(beta_diffxz) wdiffxz_sd15c07=e(beta_wdiffxz) yxz_sd15c07=e(beta_yxz) wyxz_sd15c07=e(beta_wyxz) random_sd15c07=e(beta_random) maxz_sd15c07=e(beta_maxz) ratioxy_sd15c07=e(beta_ratioxy) lijp_sd15c07=e(beta_lijp) lijp2_sd15c07=e(beta_lijp2) maxxy_sd15c07=e(beta_maxxy), reps(1000) saving(allcsd_sd15c07.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.9
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.9\0.9,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15c09=e(beta_maxy) maxx_sd15c09=e(beta_maxx) minz_sd15c09=e(beta_minz) sumyx_sd15c09=e(beta_sumyx) wsumyx_sd15c09=e(beta_wsumyx) diffyz_sd15c09=e(beta_diffyz) wdiffyz_sd15c09=e(beta_wdiffyz) diffxz_sd15c09=e(beta_diffxz) wdiffxz_sd15c09=e(beta_wdiffxz) yxz_sd15c09=e(beta_yxz) wyxz_sd15c09=e(beta_wyxz) random_sd15c09=e(beta_random) maxz_sd15c09=e(beta_maxz) ratioxy_sd15c09=e(beta_ratioxy) lijp_sd15c09=e(beta_lijp) lijp2_sd15c09=e(beta_lijp2) maxxy_sd15c09=e(beta_maxxy), reps(1000) saving(allcsd_sd15c09.dta, replace every(10)): cds2




******************************************************************************************************************

******************************************************************************************************************
******************************************************************************************************************
* change correlation x1, z, N=100, SD(x1)=2
******************************************************************************************************************

***********************************************************************************************************
* corr(x1,z)= -0.9
***********************************************************************************************************
	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.9\-0.9,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn09=e(beta_maxy) maxx_sd20cn09=e(beta_maxx) minz_sd20cn09=e(beta_minz) sumyx_sd20cn09=e(beta_sumyx) wsumyx_sd20cn09=e(beta_wsumyx) diffyz_sd20cn09=e(beta_diffyz) wdiffyz_sd20cn09=e(beta_wdiffyz) diffxz_sd20cn09=e(beta_diffxz) wdiffxz_sd20cn09=e(beta_wdiffxz) yxz_sd20cn09=e(beta_yxz) wyxz_sd20cn09=e(beta_wyxz) random_sd20cn09=e(beta_random) maxz_sd20cn09=e(beta_maxz) ratioxy_sd20cn09=e(beta_ratioxy) lijp_sd20cn09=e(beta_lijp) lijp2_sd20cn09=e(beta_lijp2) maxxy_sd20cn09=e(beta_maxxy), reps(1000) saving(allcsd_sd20cn09.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.7\-0.7,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn07=e(beta_maxy) maxx_sd20cn07=e(beta_maxx) minz_sd20cn07=e(beta_minz) sumyx_sd20cn07=e(beta_sumyx) wsumyx_sd20cn07=e(beta_wsumyx) diffyz_sd20cn07=e(beta_diffyz) wdiffyz_sd20cn07=e(beta_wdiffyz) diffxz_sd20cn07=e(beta_diffxz) wdiffxz_sd20cn07=e(beta_wdiffxz) yxz_sd20cn07=e(beta_yxz) wyxz_sd20cn07=e(beta_wyxz) random_sd20cn07=e(beta_random) maxz_sd20cn07=e(beta_maxz) ratioxy_sd20cn07=e(beta_ratioxy) lijp_sd20cn07=e(beta_lijp) lijp2_sd20cn07=e(beta_lijp2) maxxy_sd20cn07=e(beta_maxxy), reps(1000) saving(allcsd_sd20cn07.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.5
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.5\-0.5,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn05=e(beta_maxy) maxx_sd20cn05=e(beta_maxx) minz_sd20cn05=e(beta_minz) sumyx_sd20cn05=e(beta_sumyx) wsumyx_sd20cn05=e(beta_wsumyx) diffyz_sd20cn05=e(beta_diffyz) wdiffyz_sd20cn05=e(beta_wdiffyz) diffxz_sd20cn05=e(beta_diffxz) wdiffxz_sd20cn05=e(beta_wdiffxz) yxz_sd20cn05=e(beta_yxz) wyxz_sd20cn05=e(beta_wyxz) random_sd20cn05=e(beta_random) maxz_sd20cn05=e(beta_maxz) ratioxy_sd20cn05=e(beta_ratioxy) lijp_sd20cn05=e(beta_lijp) lijp2_sd20cn05=e(beta_lijp2) maxxy_sd20cn05=e(beta_maxxy), reps(1000) saving(allcsd_sd20cn05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.3
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.3\-0.3,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn03=e(beta_maxy) maxx_sd20cn03=e(beta_maxx) minz_sd20cn03=e(beta_minz) sumyx_sd20cn03=e(beta_sumyx) wsumyx_sd20cn03=e(beta_wsumyx) diffyz_sd20cn03=e(beta_diffyz) wdiffyz_sd20cn03=e(beta_wdiffyz) diffxz_sd20cn03=e(beta_diffxz) wdiffxz_sd20cn03=e(beta_wdiffxz) yxz_sd20cn03=e(beta_yxz) wyxz_sd20cn03=e(beta_wyxz) random_sd20cn03=e(beta_random) maxz_sd20cn03=e(beta_maxz) ratioxy_sd20cn03=e(beta_ratioxy) lijp_sd20cn03=e(beta_lijp) lijp2_sd20cn03=e(beta_lijp2) maxxy_sd20cn03=e(beta_maxxy), reps(1000) saving(allcsd_sd20cn03.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.1\-0.1,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn01=e(beta_maxy) maxx_sd20cn01=e(beta_maxx) minz_sd20cn01=e(beta_minz) sumyx_sd20cn01=e(beta_sumyx) wsumyx_sd20cn01=e(beta_wsumyx) diffyz_sd20cn01=e(beta_diffyz) wdiffyz_sd20cn01=e(beta_wdiffyz) diffxz_sd20cn01=e(beta_diffxz) wdiffxz_sd20cn01=e(beta_wdiffxz) yxz_sd20cn01=e(beta_yxz) wyxz_sd20cn01=e(beta_wyxz) random_sd20cn01=e(beta_random) maxz_sd20cn01=e(beta_maxz) ratioxy_sd20cn01=e(beta_ratioxy) lijp_sd20cn01=e(beta_lijp) lijp2_sd20cn01=e(beta_lijp2) maxxy_sd20cn01=e(beta_maxxy), reps(1000) saving(allcsd_sd20cn01.dta, replace every(10)): cds2





******************************************************************************************************************
******************************************************************************************************************
* corr(x1, z)=0
******************************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn00=e(beta_maxy) maxx_sd20cn00=e(beta_maxx) minz_sd20cn00=e(beta_minz) sumyx_sd20cn00=e(beta_sumyx) wsumyx_sd20cn00=e(beta_wsumyx) diffyz_sd20cn00=e(beta_diffyz) wdiffyz_sd20cn00=e(beta_wdiffyz) diffxz_sd20cn00=e(beta_diffxz) wdiffxz_sd20cn00=e(beta_wdiffxz) yxz_sd20cn00=e(beta_yxz) wyxz_sd20cn00=e(beta_wyxz) random_sd20cn00=e(beta_random) maxz_sd20cn00=e(beta_maxz) ratioxy_sd20cn00=e(beta_ratioxy) lijp_sd20cn00=e(beta_lijp) lijp2_sd20cn00=e(beta_lijp2) maxxy_sd20cn00=e(beta_maxxy), reps(1000) saving(allcsd_sd20cn00.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.1\0.1,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20c01=e(beta_maxy) maxx_sd20c01=e(beta_maxx) minz_sd20c01=e(beta_minz) sumyx_sd20c01=e(beta_sumyx) wsumyx_sd20c01=e(beta_wsumyx) diffyz_sd20c01=e(beta_diffyz) wdiffyz_sd20c01=e(beta_wdiffyz) diffxz_sd20c01=e(beta_diffxz) wdiffxz_sd20c01=e(beta_wdiffxz) yxz_sd20c01=e(beta_yxz) wyxz_sd20c01=e(beta_wyxz) random_sd20c01=e(beta_random) maxz_sd20c01=e(beta_maxz) ratioxy_sd20c01=e(beta_ratioxy) lijp_sd20c01=e(beta_lijp) lijp2_sd20c01=e(beta_lijp2) maxxy_sd20c01=e(beta_maxxy), reps(1000) saving(allcsd_sd20c01.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.3
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.3\0.3,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20c03=e(beta_maxy) maxx_sd20c03=e(beta_maxx) minz_sd20c03=e(beta_minz) sumyx_sd20c03=e(beta_sumyx) wsumyx_sd20c03=e(beta_wsumyx) diffyz_sd20c03=e(beta_diffyz) wdiffyz_sd20c03=e(beta_wdiffyz) diffxz_sd20c03=e(beta_diffxz) wdiffxz_sd20c03=e(beta_wdiffxz) yxz_sd20c03=e(beta_yxz) wyxz_sd20c03=e(beta_wyxz) random_sd20c03=e(beta_random) maxz_sd20c03=e(beta_maxz) ratioxy_sd20c03=e(beta_ratioxy) lijp_sd20c03=e(beta_lijp) lijp2_sd20c03=e(beta_lijp2) maxxy_sd20c03=e(beta_maxxy), reps(1000) saving(allcsd_sd20c03.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.5
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.5\0.5,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20c05=e(beta_maxy) maxx_sd20c05=e(beta_maxx) minz_sd20c05=e(beta_minz) sumyx_sd20c05=e(beta_sumyx) wsumyx_sd20c05=e(beta_wsumyx) diffyz_sd20c05=e(beta_diffyz) wdiffyz_sd20c05=e(beta_wdiffyz) diffxz_sd20c05=e(beta_diffxz) wdiffxz_sd20c05=e(beta_wdiffxz) yxz_sd20c05=e(beta_yxz) wyxz_sd20c05=e(beta_wyxz) random_sd20c05=e(beta_random) maxz_sd20c05=e(beta_maxz) ratioxy_sd20c05=e(beta_ratioxy) lijp_sd20c05=e(beta_lijp) lijp2_sd20c05=e(beta_lijp2) maxxy_sd20c05=e(beta_maxxy), reps(1000) saving(allcsd_sd20c05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.7\0.7,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20c07=e(beta_maxy) maxx_sd20c07=e(beta_maxx) minz_sd20c07=e(beta_minz) sumyx_sd20c07=e(beta_sumyx) wsumyx_sd20c07=e(beta_wsumyx) diffyz_sd20c07=e(beta_diffyz) wdiffyz_sd20c07=e(beta_wdiffyz) diffxz_sd20c07=e(beta_diffxz) wdiffxz_sd20c07=e(beta_wdiffxz) yxz_sd20c07=e(beta_yxz) wyxz_sd20c07=e(beta_wyxz) random_sd20c07=e(beta_random) maxz_sd20c07=e(beta_maxz) ratioxy_sd20c07=e(beta_ratioxy) lijp_sd20c07=e(beta_lijp) lijp2_sd20c07=e(beta_lijp2) maxxy_sd20c07=e(beta_maxxy), reps(1000) saving(allcsd_sd20c07.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.9
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.9\0.9,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20c09=e(beta_maxy) maxx_sd20c09=e(beta_maxx) minz_sd20c09=e(beta_minz) sumyx_sd20c09=e(beta_sumyx) wsumyx_sd20c09=e(beta_wsumyx) diffyz_sd20c09=e(beta_diffyz) wdiffyz_sd20c09=e(beta_wdiffyz) diffxz_sd20c09=e(beta_diffxz) wdiffxz_sd20c09=e(beta_wdiffxz) yxz_sd20c09=e(beta_yxz) wyxz_sd20c09=e(beta_wyxz) random_sd20c09=e(beta_random) maxz_sd20c09=e(beta_maxz) ratioxy_sd20c09=e(beta_ratioxy) lijp_sd20c09=e(beta_lijp) lijp2_sd20c09=e(beta_lijp2) maxxy_sd20c09=e(beta_maxxy), reps(1000) saving(allcsd_sd20c09.dta, replace every(10)): cds2




******************************************************************************************************************

******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************


******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************
******************************************************************************

	
	clear
	use allcsd_N20.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_N20.dta, replace


	tempvar p1 p2 p3 
	sum maxy_N20
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_N20-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_N20
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_N20-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_N20
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_N20-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_N20
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_N20-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_N20
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_N20-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_N20
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_N20-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_N20
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_N20-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_N20
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_N20-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_N20
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_N20-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_N20
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_N20-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_N20
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_N20-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_N20
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_N20-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_N20
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_N20-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_N20
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_N20-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_N20
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_N20-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_N20
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_N20-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_N20
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_N20-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy

************************************************************************************************************************************************************

	clear
	use allcsd_N40.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_N40.dta, replace


	tempvar p1 p2 p3 
	sum maxy_N40
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_N40-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_N40
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_N40-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_N40
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_N40-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_N40
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_N40-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_N40
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_N40-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_N40
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_N40-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_N40
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_N40-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_N40
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_N40-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_N40
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_N40-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_N40
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_N40-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_N40
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_N40-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_N40
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_N40-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_N40
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_N40-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_N40
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_N40-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_N40
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_N40-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_N40
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_N40-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_N40
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_N40-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_N60.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_N60.dta, replace


	tempvar p1 p2 p3 
	sum maxy_N60
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_N60-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_N60
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_N60-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_N60
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_N60-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_N60
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_N60-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_N60
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_N60-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_N60
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_N60-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_N60
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_N60-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_N60
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_N60-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_N60
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_N60-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_N60
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_N60-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_N60
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_N60-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_N60
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_N60-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_N60
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_N60-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_N60
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_N60-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_N60
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_N60-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_N60
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_N60-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_N60
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_N60-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_N80.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_N80.dta, replace


	tempvar p1 p2 p3 
	sum maxy_N80
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_N80-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_N80
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_N80-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_N80
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_N80-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_N80
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_N80-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_N80
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_N80-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_N80
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_N80-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_N80
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_N80-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_N80
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_N80-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_N80
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_N80-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_N80
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_N80-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_N80
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_N80-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_N80
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_N80-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_N80
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_N80-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_N80
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_N80-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_N80
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_N80-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_N80
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_N80-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_N80
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_N80-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_N100.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_N100.dta, replace


	tempvar p1 p2 p3 
	sum maxy_N100
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_N100-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_N100
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_N100-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_N100
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_N100-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_N100
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_N100-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_N100
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_N100-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_N100
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_N100-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_N100
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_N100-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_N100
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_N100-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_N100
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_N100-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_N100
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_N100-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_N100
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_N100-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_N100
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_N100-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_N100
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_N100-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_N100
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_N100-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_N100
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_N100-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_N100
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_N100-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_N100
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_N100-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************
************************************************************************************************************************************************************

	clear
	use allcsd_sd03cn09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03cn09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_sd03cn07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03cn07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn07	
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd03cn05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03cn05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd03cn03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03cn03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd03cn01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03cn01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'



	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd03cn00.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03cn00.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn00
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn00-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn00
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn00-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn00
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn00-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn00
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn00-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn00
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn00-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn00
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn00-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn00
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn00-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn00
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn00-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn00
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn00-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn00
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn00-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn00
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn00-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn00
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn00-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn00
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn00-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn00
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn00-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn00
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn00-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn00
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn00-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn00
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn00-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'



	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd03c01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03c01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03c01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03c01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03c01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03c01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03c01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03c01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03c01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03c01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03c01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03c01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03c01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03c01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03c01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03c01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03c01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03c01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03c01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03c01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03c01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03c01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03c01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03c01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03c01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03c01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03c01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03c01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03c01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03c01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03c01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03c01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03c01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03c01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03c01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03c01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd03c03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03c03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03c03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03c03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03c03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03c03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03c03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03c03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03c03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03c03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03c03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03c03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03c03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03c03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03c03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03c03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03c03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03c03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03c03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03c03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03c03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03c03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03c03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03c03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03c03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03c03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03c03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03c03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03c03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03c03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03c03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03c03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03c03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03c03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03c03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03c03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd03c05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03c05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03c05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03c05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03c05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03c05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03c05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03c05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03c05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03c05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03c05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03c05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03c05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03c05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03c05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03c05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03c05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03c05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03c05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03c05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03c05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03c05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03c05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03c05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03c05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03c05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03c05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03c05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03c05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03c05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03c05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03c05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03c05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03c05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03c05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03c05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd03c07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03c07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03c07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03c07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03c07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03c07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03c07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03c07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03c07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03c07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03c07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03c07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03c07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03c07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03c07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03c07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03c07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03c07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03c07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03c07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03c07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03c07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03c07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03c07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03c07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03c07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03c07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03c07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03c07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03c07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03c07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03c07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03c07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03c07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03c07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03c07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd03c09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd03c09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03c09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03c09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03c09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03c09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03c09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03c09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03c09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03c09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03c09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03c09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03c09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03c09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03c09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03c09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03c09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03c09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03c09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03c09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03c09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03c09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03c09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03c09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03c09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03c09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03c09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03c09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03c09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03c09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03c09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03c09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03c09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03c09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03c09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03c09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************
************************************************************************************************************************************************************

	clear
	use allcsd_sd07cn09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07cn09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_sd07cn07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07cn07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd07cn05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07cn05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd07cn03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07cn03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd07cn01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07cn01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd07cn00.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07cn00.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn00
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn00-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn00
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn00-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn00
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn00-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn00
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn00-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn00
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn00-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn00
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn00-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn00
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn00-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn00
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn00-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn00
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn00-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn00
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn00-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn00
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn00-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn00
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn00-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn00
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn00-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn00
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn00-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn00
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn00-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn00
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn00-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn00
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn00-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd07c01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07c01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07c01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07c01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07c01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07c01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07c01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07c01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07c01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07c01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07c01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07c01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07c01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07c01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07c01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07c01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07c01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07c01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07c01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07c01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07c01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07c01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07c01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07c01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07c01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07c01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07c01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07c01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07c01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07c01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07c01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07c01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07c01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07c01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07c01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07c01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd07c03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07c03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07c03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07c03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07c03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07c03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07c03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07c03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07c03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07c03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07c03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07c03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07c03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07c03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07c03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07c03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07c03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07c03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07c03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07c03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07c03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07c03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07c03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07c03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07c03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07c03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07c03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07c03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07c03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07c03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07c03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07c03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07c03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07c03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07c03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07c03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd07c05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07c05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07c05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07c05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07c05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07c05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07c05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07c05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07c05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07c05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07c05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07c05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07c05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07c05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07c05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07c05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07c05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07c05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07c05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07c05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07c05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07c05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07c05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07c05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07c05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07c05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07c05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07c05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07c05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07c05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07c05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07c05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07c05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07c05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07c05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07c05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd07c07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07c07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07c07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07c07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07c07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07c07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07c07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07c07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07c07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07c07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07c07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07c07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07c07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07c07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07c07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07c07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07c07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07c07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07c07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07c07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07c07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07c07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07c07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07c07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07c07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07c07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07c07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07c07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07c07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07c07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07c07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07c07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07c07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07c07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07c07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07c07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd07c09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd07c09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07c09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07c09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07c09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07c09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07c09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07c09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07c09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07c09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07c09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07c09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07c09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07c09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07c09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07c09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07c09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07c09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07c09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07c09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07c09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07c09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07c09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07c09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07c09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07c09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07c09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07c09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07c09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07c09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07c09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07c09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07c09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07c09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07c09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07c09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************
************************************************************************************************************************************************************

	clear
	use allcsd_sd10cn09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10cn09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'



	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_sd10cn07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10cn07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd10cn05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10cn05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd10cn03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10cn03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd10cn01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10cn01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd10cn00.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10cn00.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn00
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn00-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn00
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn00-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn00
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn00-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn00
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn00-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn00
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn00-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn00
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn00-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn00
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn00-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn00
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn00-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn00
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn00-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn00
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn00-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn00
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn00-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn00
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn00-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn00
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn00-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn00
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn00-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn00
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn00-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn00
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn00-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn00
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn00-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd10c01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10c01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10c01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10c01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10c01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10c01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10c01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10c01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10c01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10c01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10c01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10c01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10c01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10c01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10c01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10c01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10c01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10c01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10c01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10c01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10c01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10c01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10c01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10c01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10c01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10c01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10c01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10c01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10c01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10c01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10c01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10c01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10c01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10c01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10c01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10c01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd10c03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10c03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10c03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10c03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10c03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10c03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10c03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10c03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10c03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10c03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10c03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10c03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10c03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10c03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10c03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10c03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10c03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10c03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10c03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10c03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10c03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10c03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10c03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10c03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10c03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10c03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10c03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10c03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10c03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10c03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10c03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10c03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10c03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10c03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10c03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10c03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd10c05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10c05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10c05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10c05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10c05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10c05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10c05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10c05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10c05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10c05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10c05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10c05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10c05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10c05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10c05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10c05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10c05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10c05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10c05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10c05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10c05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10c05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10c05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10c05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10c05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10c05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10c05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10c05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10c05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10c05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10c05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10c05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10c05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10c05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10c05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10c05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd10c07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10c07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10c07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10c07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10c07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10c07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10c07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10c07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10c07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10c07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10c07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10c07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10c07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10c07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10c07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10c07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10c07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10c07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10c07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10c07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10c07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10c07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10c07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10c07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10c07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10c07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10c07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10c07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10c07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10c07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10c07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10c07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10c07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10c07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10c07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10c07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd10c09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd10c09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10c09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10c09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10c09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10c09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10c09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10c09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10c09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10c09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10c09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10c09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10c09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10c09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10c09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10c09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10c09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10c09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10c09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10c09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10c09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10c09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10c09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10c09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10c09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10c09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10c09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10c09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10c09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10c09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10c09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10c09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10c09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10c09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10c09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10c09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************
************************************************************************************************************************************************************

	clear

	use allcsd_sd15cn09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15cn09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_sd15cn07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15cn07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd15cn05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15cn05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd15cn03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15cn03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd15cn01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15cn01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd15cn00.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15cn00.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn00
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn00-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn00
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn00-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn00
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn00-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn00
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn00-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn00
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn00-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn00
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn00-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn00
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn00-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn00
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn00-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn00
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn00-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn00
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn00-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn00
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn00-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn00
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn00-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn00
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn00-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn00
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn00-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn00
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn00-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn00
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn00-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn00
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn00-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd15c01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15c01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15c01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15c01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15c01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15c01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15c01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15c01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15c01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15c01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15c01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15c01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15c01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15c01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15c01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15c01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15c01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15c01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15c01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15c01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15c01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15c01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15c01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15c01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15c01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15c01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15c01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15c01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15c01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15c01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15c01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15c01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15c01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15c01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15c01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15c01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd15c03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15c03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15c03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15c03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15c03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15c03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15c03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15c03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15c03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15c03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15c03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15c03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15c03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15c03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15c03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15c03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15c03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15c03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15c03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15c03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15c03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15c03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15c03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15c03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15c03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15c03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15c03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15c03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15c03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15c03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15c03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15c03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15c03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15c03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15c03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15c03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd15c05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15c05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15c05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15c05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15c05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15c05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15c05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15c05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15c05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15c05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15c05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15c05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15c05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15c05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15c05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15c05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15c05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15c05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15c05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15c05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15c05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15c05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15c05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15c05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15c05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15c05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15c05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15c05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15c05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15c05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15c05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15c05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15c05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15c05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15c05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15c05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd15c07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15c07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15c07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15c07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15c07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15c07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15c07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15c07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15c07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15c07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15c07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15c07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15c07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15c07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15c07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15c07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15c07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15c07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15c07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15c07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15c07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15c07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15c07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15c07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15c07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15c07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15c07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15c07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15c07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15c07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15c07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15c07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15c07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15c07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15c07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15c07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd15c09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd15c09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15c09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15c09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15c09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15c09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15c09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15c09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15c09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15c09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15c09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15c09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15c09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15c09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15c09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15c09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15c09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15c09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15c09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15c09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15c09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15c09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15c09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15c09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15c09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15c09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15c09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15c09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15c09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15c09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15c09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15c09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15c09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15c09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15c09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15c09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************
************************************************************************************************************************************************************


	clear
	use allcsd_sd20cn09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20cn09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_sd20cn07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20cn07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd20cn05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20cn05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd20cn03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20cn03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd20cn01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20cn01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd20cn00.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20cn00.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn00
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn00-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn00
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn00-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn00
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn00-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn00
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn00-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn00
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn00-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn00
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn00-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn00
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn00-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn00
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn00-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn00
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn00-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn00
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn00-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn00
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn00-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn00
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn00-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn00
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn00-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn00
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn00-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn00
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn00-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn00
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn00-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn00
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn00-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd20c01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20c01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20c01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20c01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20c01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20c01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20c01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20c01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20c01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20c01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20c01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20c01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20c01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20c01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20c01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20c01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20c01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20c01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20c01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20c01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20c01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20c01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20c01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20c01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20c01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20c01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20c01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20c01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20c01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20c01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20c01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20c01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20c01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20c01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20c01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20c01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd20c03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20c03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20c03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20c03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20c03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20c03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20c03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20c03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20c03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20c03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20c03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20c03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20c03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20c03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20c03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20c03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20c03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20c03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20c03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20c03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20c03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20c03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20c03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20c03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20c03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20c03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20c03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20c03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20c03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20c03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20c03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20c03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20c03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20c03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20c03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20c03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd20c05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20c05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20c05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20c05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20c05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20c05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20c05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20c05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20c05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20c05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20c05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20c05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20c05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20c05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20c05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20c05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20c05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20c05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20c05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20c05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20c05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20c05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20c05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20c05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20c05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20c05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20c05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20c05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20c05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20c05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20c05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20c05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20c05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20c05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20c05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20c05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd20c07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20c07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20c07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20c07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20c07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20c07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20c07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20c07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20c07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20c07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20c07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20c07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20c07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20c07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20c07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20c07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20c07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20c07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20c07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20c07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20c07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20c07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20c07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20c07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20c07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20c07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20c07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20c07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20c07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20c07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20c07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20c07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20c07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20c07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20c07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20c07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_sd20c09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_cont_allcsd_sd20c09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20c09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20c09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20c09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20c09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20c09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20c09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20c09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20c09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20c09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20c09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20c09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20c09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20c09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20c09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20c09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20c09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20c09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20c09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20c09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20c09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20c09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20c09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20c09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20c09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20c09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20c09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20c09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20c09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20c09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20c09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20c09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20c09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20c09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20c09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy

	clear

************************************************************************************************************************************************************
************************************************************************************************************************************************************
************************************************************************************************************************************************************
************************************************************************************************************************************************************
************************************************************************************************************************************************************
* B: experiments with binary RHS variables	
********************************************************************************************
* variation of no of obs; corr=0, SD(x1, z)=1
* 1. N=20
********************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 20
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/20 {

  		forvalues j=1/20 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'
	
	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 20
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)


* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)


	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2


* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)

	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

		
	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy


	end
	
	

	simulate maxy_N20=e(beta_maxy) maxx_N20=e(beta_maxx) minz_N20=e(beta_minz) sumyx_N20=e(beta_sumyx) wsumyx_N20=e(beta_wsumyx) diffyz_N20=e(beta_diffyz) wdiffyz_N20=e(beta_wdiffyz) diffxz_N20=e(beta_diffxz) wdiffxz_N20=e(beta_wdiffxz) yxz_N20=e(beta_yxz) wyxz_N20=e(beta_wyxz) random_N20=e(beta_random) maxz_N20=e(beta_maxz) ratioxy_N20=e(beta_ratioxy) lijp_N20=e(beta_lijp) lijp2_N20=e(beta_lijp2) maxxy_N20=e(beta_maxxy), reps(1000) saving(allcsd_bin_N20.dta, replace every(10)): cds2

******************************************************************************************************************************************************************************************************************************************************************************
******************************************************************************************************************************************************************************************************************************************************************************
* 2. N=40
******************************************************************************************************************************************************************************************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 40
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/40 {

  		forvalues j=1/40 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 40
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_N40=e(beta_maxy) maxx_N40=e(beta_maxx) minz_N40=e(beta_minz) sumyx_N40=e(beta_sumyx) wsumyx_N40=e(beta_wsumyx) diffyz_N40=e(beta_diffyz) wdiffyz_N40=e(beta_wdiffyz) diffxz_N40=e(beta_diffxz) wdiffxz_N40=e(beta_wdiffxz) yxz_N40=e(beta_yxz) wyxz_N40=e(beta_wyxz) random_N40=e(beta_random) maxz_N40=e(beta_maxz) ratioxy_N40=e(beta_ratioxy) lijp_N40=e(beta_lijp) lijp2_N40=e(beta_lijp2) maxxy_N40=e(beta_maxxy), reps(1000) saving(allcsd_bin_N40.dta, replace every(10)): cds2





****************************************************************************************************
****************************************************************************************************
* 3. N=60
****************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 60
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/60 {

  		forvalues j=1/60 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 60
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_N60=e(beta_maxy) maxx_N60=e(beta_maxx) minz_N60=e(beta_minz) sumyx_N60=e(beta_sumyx) wsumyx_N60=e(beta_wsumyx) diffyz_N60=e(beta_diffyz) wdiffyz_N60=e(beta_wdiffyz) diffxz_N60=e(beta_diffxz) wdiffxz_N60=e(beta_wdiffxz) yxz_N60=e(beta_yxz) wyxz_N60=e(beta_wyxz) random_N60=e(beta_random) maxz_N60=e(beta_maxz) ratioxy_N60=e(beta_ratioxy) lijp_N60=e(beta_lijp) lijp2_N60=e(beta_lijp2) maxxy_N60=e(beta_maxxy), reps(1000) saving(allcsd_bin_N60.dta, replace every(10)): cds2





****************************************************************************************************
****************************************************************************************************
* 4. N=80
****************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 80
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/80 {

  		forvalues j=1/80 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 80
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_N80=e(beta_maxy) maxx_N80=e(beta_maxx) minz_N80=e(beta_minz) sumyx_N80=e(beta_sumyx) wsumyx_N80=e(beta_wsumyx) diffyz_N80=e(beta_diffyz) wdiffyz_N80=e(beta_wdiffyz) diffxz_N80=e(beta_diffxz) wdiffxz_N80=e(beta_wdiffxz) yxz_N80=e(beta_yxz) wyxz_N80=e(beta_wyxz) random_N80=e(beta_random) maxz_N80=e(beta_maxz) ratioxy_N80=e(beta_ratioxy) lijp_N80=e(beta_lijp) lijp2_N80=e(beta_lijp2) maxxy_N80=e(beta_maxxy), reps(1000) saving(allcsd_bin_N80.dta, replace every(10)): cds2




****************************************************************************************************
****************************************************************************************************
* 5. N=100
****************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_N100=e(beta_maxy) maxx_N100=e(beta_maxx) minz_N100=e(beta_minz) sumyx_N100=e(beta_sumyx) wsumyx_N100=e(beta_wsumyx) diffyz_N100=e(beta_diffyz) wdiffyz_N100=e(beta_wdiffyz) diffxz_N100=e(beta_diffxz) wdiffxz_N100=e(beta_wdiffxz) yxz_N100=e(beta_yxz) wyxz_N100=e(beta_wyxz) random_N100=e(beta_random) maxz_N100=e(beta_maxz) ratioxy_N100=e(beta_ratioxy) lijp_N100=e(beta_lijp) lijp2_N100=e(beta_lijp2) maxxy_N100=e(beta_maxxy), reps(1000) saving(allcsd_bin_N100.dta, replace every(10)): cds2



******************************************************************************************************************


******************************************************************************************************************
******************************************************************************************************************
* change correlation x1, z, N=100, SD(x1)=0.3
******************************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.9
***********************************************************************************************************
	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.9\-0.9,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn09=e(beta_maxy) maxx_sd03cn09=e(beta_maxx) minz_sd03cn09=e(beta_minz) sumyx_sd03cn09=e(beta_sumyx) wsumyx_sd03cn09=e(beta_wsumyx) diffyz_sd03cn09=e(beta_diffyz) wdiffyz_sd03cn09=e(beta_wdiffyz) diffxz_sd03cn09=e(beta_diffxz) wdiffxz_sd03cn09=e(beta_wdiffxz) yxz_sd03cn09=e(beta_yxz) wyxz_sd03cn09=e(beta_wyxz) random_sd03cn09=e(beta_random) maxz_sd03cn09=e(beta_maxz) ratioxy_sd03cn09=e(beta_ratioxy) lijp_sd03cn09=e(beta_lijp) lijp2_sd03cn09=e(beta_lijp2) maxxy_sd03cn09=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03cn09.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.7\-0.7,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn07=e(beta_maxy) maxx_sd03cn07=e(beta_maxx) minz_sd03cn07=e(beta_minz) sumyx_sd03cn07=e(beta_sumyx) wsumyx_sd03cn07=e(beta_wsumyx) diffyz_sd03cn07=e(beta_diffyz) wdiffyz_sd03cn07=e(beta_wdiffyz) diffxz_sd03cn07=e(beta_diffxz) wdiffxz_sd03cn07=e(beta_wdiffxz) yxz_sd03cn07=e(beta_yxz) wyxz_sd03cn07=e(beta_wyxz) random_sd03cn07=e(beta_random) maxz_sd03cn07=e(beta_maxz) ratioxy_sd03cn07=e(beta_ratioxy) lijp_sd03cn07=e(beta_lijp) lijp2_sd03cn07=e(beta_lijp2) maxxy_sd03cn07=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03cn07.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.5
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.5\-0.5,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn05=e(beta_maxy) maxx_sd03cn05=e(beta_maxx) minz_sd03cn05=e(beta_minz) sumyx_sd03cn05=e(beta_sumyx) wsumyx_sd03cn05=e(beta_wsumyx) diffyz_sd03cn05=e(beta_diffyz) wdiffyz_sd03cn05=e(beta_wdiffyz) diffxz_sd03cn05=e(beta_diffxz) wdiffxz_sd03cn05=e(beta_wdiffxz) yxz_sd03cn05=e(beta_yxz) wyxz_sd03cn05=e(beta_wyxz) random_sd03cn05=e(beta_random) maxz_sd03cn05=e(beta_maxz) ratioxy_sd03cn05=e(beta_ratioxy) lijp_sd03cn05=e(beta_lijp) lijp2_sd03cn05=e(beta_lijp2) maxxy_sd03cn05=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03cn05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.3
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.3\-0.3,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn03=e(beta_maxy) maxx_sd03cn03=e(beta_maxx) minz_sd03cn03=e(beta_minz) sumyx_sd03cn03=e(beta_sumyx) wsumyx_sd03cn03=e(beta_wsumyx) diffyz_sd03cn03=e(beta_diffyz) wdiffyz_sd03cn03=e(beta_wdiffyz) diffxz_sd03cn03=e(beta_diffxz) wdiffxz_sd03cn03=e(beta_wdiffxz) yxz_sd03cn03=e(beta_yxz) wyxz_sd03cn03=e(beta_wyxz) random_sd03cn03=e(beta_random) maxz_sd03cn03=e(beta_maxz) ratioxy_sd03cn03=e(beta_ratioxy) lijp_sd03cn03=e(beta_lijp) lijp2_sd03cn03=e(beta_lijp2) maxxy_sd03cn03=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03cn03.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.1\-0.1,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn01=e(beta_maxy) maxx_sd03cn01=e(beta_maxx) minz_sd03cn01=e(beta_minz) sumyx_sd03cn01=e(beta_sumyx) wsumyx_sd03cn01=e(beta_wsumyx) diffyz_sd03cn01=e(beta_diffyz) wdiffyz_sd03cn01=e(beta_wdiffyz) diffxz_sd03cn01=e(beta_diffxz) wdiffxz_sd03cn01=e(beta_wdiffxz) yxz_sd03cn01=e(beta_yxz) wyxz_sd03cn01=e(beta_wyxz) random_sd03cn01=e(beta_random) maxz_sd03cn01=e(beta_maxz) ratioxy_sd03cn01=e(beta_ratioxy) lijp_sd03cn01=e(beta_lijp) lijp2_sd03cn01=e(beta_lijp2) maxxy_sd03cn01=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03cn01.dta, replace every(10)): cds2





******************************************************************************************************************
******************************************************************************************************************
* corr(x1, z)=0
******************************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03cn00=e(beta_maxy) maxx_sd03cn00=e(beta_maxx) minz_sd03cn00=e(beta_minz) sumyx_sd03cn00=e(beta_sumyx) wsumyx_sd03cn00=e(beta_wsumyx) diffyz_sd03cn00=e(beta_diffyz) wdiffyz_sd03cn00=e(beta_wdiffyz) diffxz_sd03cn00=e(beta_diffxz) wdiffxz_sd03cn00=e(beta_wdiffxz) yxz_sd03cn00=e(beta_yxz) wyxz_sd03cn00=e(beta_wyxz) random_sd03cn00=e(beta_random) maxz_sd03cn00=e(beta_maxz) ratioxy_sd03cn00=e(beta_ratioxy) lijp_sd03cn00=e(beta_lijp) lijp2_sd03cn00=e(beta_lijp2) maxxy_sd03cn00=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03cn00.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.1\0.1,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03c01=e(beta_maxy) maxx_sd03c01=e(beta_maxx) minz_sd03c01=e(beta_minz) sumyx_sd03c01=e(beta_sumyx) wsumyx_sd03c01=e(beta_wsumyx) diffyz_sd03c01=e(beta_diffyz) wdiffyz_sd03c01=e(beta_wdiffyz) diffxz_sd03c01=e(beta_diffxz) wdiffxz_sd03c01=e(beta_wdiffxz) yxz_sd03c01=e(beta_yxz) wyxz_sd03c01=e(beta_wyxz) random_sd03c01=e(beta_random) maxz_sd03c01=e(beta_maxz) ratioxy_sd03c01=e(beta_ratioxy) lijp_sd03c01=e(beta_lijp) lijp2_sd03c01=e(beta_lijp2) maxxy_sd03c01=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03c01.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.3
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.3\0.3,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03c03=e(beta_maxy) maxx_sd03c03=e(beta_maxx) minz_sd03c03=e(beta_minz) sumyx_sd03c03=e(beta_sumyx) wsumyx_sd03c03=e(beta_wsumyx) diffyz_sd03c03=e(beta_diffyz) wdiffyz_sd03c03=e(beta_wdiffyz) diffxz_sd03c03=e(beta_diffxz) wdiffxz_sd03c03=e(beta_wdiffxz) yxz_sd03c03=e(beta_yxz) wyxz_sd03c03=e(beta_wyxz) random_sd03c03=e(beta_random) maxz_sd03c03=e(beta_maxz) ratioxy_sd03c03=e(beta_ratioxy) lijp_sd03c03=e(beta_lijp) lijp2_sd03c03=e(beta_lijp2) maxxy_sd03c03=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03c03.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.5
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.5\0.5,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03c05=e(beta_maxy) maxx_sd03c05=e(beta_maxx) minz_sd03c05=e(beta_minz) sumyx_sd03c05=e(beta_sumyx) wsumyx_sd03c05=e(beta_wsumyx) diffyz_sd03c05=e(beta_diffyz) wdiffyz_sd03c05=e(beta_wdiffyz) diffxz_sd03c05=e(beta_diffxz) wdiffxz_sd03c05=e(beta_wdiffxz) yxz_sd03c05=e(beta_yxz) wyxz_sd03c05=e(beta_wyxz) random_sd03c05=e(beta_random) maxz_sd03c05=e(beta_maxz) ratioxy_sd03c05=e(beta_ratioxy) lijp_sd03c05=e(beta_lijp) lijp2_sd03c05=e(beta_lijp2) maxxy_sd03c05=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03c05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.7\0.7,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03c07=e(beta_maxy) maxx_sd03c07=e(beta_maxx) minz_sd03c07=e(beta_minz) sumyx_sd03c07=e(beta_sumyx) wsumyx_sd03c07=e(beta_wsumyx) diffyz_sd03c07=e(beta_diffyz) wdiffyz_sd03c07=e(beta_wdiffyz) diffxz_sd03c07=e(beta_diffxz) wdiffxz_sd03c07=e(beta_wdiffxz) yxz_sd03c07=e(beta_yxz) wyxz_sd03c07=e(beta_wyxz) random_sd03c07=e(beta_random) maxz_sd03c07=e(beta_maxz) ratioxy_sd03c07=e(beta_ratioxy) lijp_sd03c07=e(beta_lijp) lijp2_sd03c07=e(beta_lijp2) maxxy_sd03c07=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03c07.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.9
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.9\0.9,1)  sds(0.3,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd03c09=e(beta_maxy) maxx_sd03c09=e(beta_maxx) minz_sd03c09=e(beta_minz) sumyx_sd03c09=e(beta_sumyx) wsumyx_sd03c09=e(beta_wsumyx) diffyz_sd03c09=e(beta_diffyz) wdiffyz_sd03c09=e(beta_wdiffyz) diffxz_sd03c09=e(beta_diffxz) wdiffxz_sd03c09=e(beta_wdiffxz) yxz_sd03c09=e(beta_yxz) wyxz_sd03c09=e(beta_wyxz) random_sd03c09=e(beta_random) maxz_sd03c09=e(beta_maxz) ratioxy_sd03c09=e(beta_ratioxy) lijp_sd03c09=e(beta_lijp) lijp2_sd03c09=e(beta_lijp2) maxxy_sd03c09=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd03c09.dta, replace every(10)): cds2




******************************************************************************************************************


******************************************************************************************************************
******************************************************************************************************************
* change correlation x1, z, N=100, SD(x1)=0.7
******************************************************************************************************************

***********************************************************************************************************
* corr(x1,z)= -0.9
***********************************************************************************************************
	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.9\-0.9,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn09=e(beta_maxy) maxx_sd07cn09=e(beta_maxx) minz_sd07cn09=e(beta_minz) sumyx_sd07cn09=e(beta_sumyx) wsumyx_sd07cn09=e(beta_wsumyx) diffyz_sd07cn09=e(beta_diffyz) wdiffyz_sd07cn09=e(beta_wdiffyz) diffxz_sd07cn09=e(beta_diffxz) wdiffxz_sd07cn09=e(beta_wdiffxz) yxz_sd07cn09=e(beta_yxz) wyxz_sd07cn09=e(beta_wyxz) random_sd07cn09=e(beta_random) maxz_sd07cn09=e(beta_maxz) ratioxy_sd07cn09=e(beta_ratioxy) lijp_sd07cn09=e(beta_lijp) lijp2_sd07cn09=e(beta_lijp2) maxxy_sd07cn09=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07cn09.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.7\-0.7,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn07=e(beta_maxy) maxx_sd07cn07=e(beta_maxx) minz_sd07cn07=e(beta_minz) sumyx_sd07cn07=e(beta_sumyx) wsumyx_sd07cn07=e(beta_wsumyx) diffyz_sd07cn07=e(beta_diffyz) wdiffyz_sd07cn07=e(beta_wdiffyz) diffxz_sd07cn07=e(beta_diffxz) wdiffxz_sd07cn07=e(beta_wdiffxz) yxz_sd07cn07=e(beta_yxz) wyxz_sd07cn07=e(beta_wyxz) random_sd07cn07=e(beta_random) maxz_sd07cn07=e(beta_maxz) ratioxy_sd07cn07=e(beta_ratioxy) lijp_sd07cn07=e(beta_lijp) lijp2_sd07cn07=e(beta_lijp2) maxxy_sd07cn07=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07cn07.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.5
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.5\-0.5,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn05=e(beta_maxy) maxx_sd07cn05=e(beta_maxx) minz_sd07cn05=e(beta_minz) sumyx_sd07cn05=e(beta_sumyx) wsumyx_sd07cn05=e(beta_wsumyx) diffyz_sd07cn05=e(beta_diffyz) wdiffyz_sd07cn05=e(beta_wdiffyz) diffxz_sd07cn05=e(beta_diffxz) wdiffxz_sd07cn05=e(beta_wdiffxz) yxz_sd07cn05=e(beta_yxz) wyxz_sd07cn05=e(beta_wyxz) random_sd07cn05=e(beta_random) maxz_sd07cn05=e(beta_maxz) ratioxy_sd07cn05=e(beta_ratioxy) lijp_sd07cn05=e(beta_lijp) lijp2_sd07cn05=e(beta_lijp2) maxxy_sd07cn05=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07cn05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.3
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.3\-0.3,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn03=e(beta_maxy) maxx_sd07cn03=e(beta_maxx) minz_sd07cn03=e(beta_minz) sumyx_sd07cn03=e(beta_sumyx) wsumyx_sd07cn03=e(beta_wsumyx) diffyz_sd07cn03=e(beta_diffyz) wdiffyz_sd07cn03=e(beta_wdiffyz) diffxz_sd07cn03=e(beta_diffxz) wdiffxz_sd07cn03=e(beta_wdiffxz) yxz_sd07cn03=e(beta_yxz) wyxz_sd07cn03=e(beta_wyxz) random_sd07cn03=e(beta_random) maxz_sd07cn03=e(beta_maxz) ratioxy_sd07cn03=e(beta_ratioxy) lijp_sd07cn03=e(beta_lijp) lijp2_sd07cn03=e(beta_lijp2) maxxy_sd07cn03=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07cn03.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.1\-0.1,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn01=e(beta_maxy) maxx_sd07cn01=e(beta_maxx) minz_sd07cn01=e(beta_minz) sumyx_sd07cn01=e(beta_sumyx) wsumyx_sd07cn01=e(beta_wsumyx) diffyz_sd07cn01=e(beta_diffyz) wdiffyz_sd07cn01=e(beta_wdiffyz) diffxz_sd07cn01=e(beta_diffxz) wdiffxz_sd07cn01=e(beta_wdiffxz) yxz_sd07cn01=e(beta_yxz) wyxz_sd07cn01=e(beta_wyxz) random_sd07cn01=e(beta_random) maxz_sd07cn01=e(beta_maxz) ratioxy_sd07cn01=e(beta_ratioxy) lijp_sd07cn01=e(beta_lijp) lijp2_sd07cn01=e(beta_lijp2) maxxy_sd07cn01=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07cn01.dta, replace every(10)): cds2





******************************************************************************************************************
******************************************************************************************************************
* corr(x1, z)=0
******************************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07cn00=e(beta_maxy) maxx_sd07cn00=e(beta_maxx) minz_sd07cn00=e(beta_minz) sumyx_sd07cn00=e(beta_sumyx) wsumyx_sd07cn00=e(beta_wsumyx) diffyz_sd07cn00=e(beta_diffyz) wdiffyz_sd07cn00=e(beta_wdiffyz) diffxz_sd07cn00=e(beta_diffxz) wdiffxz_sd07cn00=e(beta_wdiffxz) yxz_sd07cn00=e(beta_yxz) wyxz_sd07cn00=e(beta_wyxz) random_sd07cn00=e(beta_random) maxz_sd07cn00=e(beta_maxz) ratioxy_sd07cn00=e(beta_ratioxy) lijp_sd07cn00=e(beta_lijp) lijp2_sd07cn00=e(beta_lijp2) maxxy_sd07cn00=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07cn00.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.1\0.1,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07c01=e(beta_maxy) maxx_sd07c01=e(beta_maxx) minz_sd07c01=e(beta_minz) sumyx_sd07c01=e(beta_sumyx) wsumyx_sd07c01=e(beta_wsumyx) diffyz_sd07c01=e(beta_diffyz) wdiffyz_sd07c01=e(beta_wdiffyz) diffxz_sd07c01=e(beta_diffxz) wdiffxz_sd07c01=e(beta_wdiffxz) yxz_sd07c01=e(beta_yxz) wyxz_sd07c01=e(beta_wyxz) random_sd07c01=e(beta_random) maxz_sd07c01=e(beta_maxz) ratioxy_sd07c01=e(beta_ratioxy) lijp_sd07c01=e(beta_lijp) lijp2_sd07c01=e(beta_lijp2) maxxy_sd07c01=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07c01.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.3
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.3\0.3,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07c03=e(beta_maxy) maxx_sd07c03=e(beta_maxx) minz_sd07c03=e(beta_minz) sumyx_sd07c03=e(beta_sumyx) wsumyx_sd07c03=e(beta_wsumyx) diffyz_sd07c03=e(beta_diffyz) wdiffyz_sd07c03=e(beta_wdiffyz) diffxz_sd07c03=e(beta_diffxz) wdiffxz_sd07c03=e(beta_wdiffxz) yxz_sd07c03=e(beta_yxz) wyxz_sd07c03=e(beta_wyxz) random_sd07c03=e(beta_random) maxz_sd07c03=e(beta_maxz) ratioxy_sd07c03=e(beta_ratioxy) lijp_sd07c03=e(beta_lijp) lijp2_sd07c03=e(beta_lijp2) maxxy_sd07c03=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07c03.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.5
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.5\0.5,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07c05=e(beta_maxy) maxx_sd07c05=e(beta_maxx) minz_sd07c05=e(beta_minz) sumyx_sd07c05=e(beta_sumyx) wsumyx_sd07c05=e(beta_wsumyx) diffyz_sd07c05=e(beta_diffyz) wdiffyz_sd07c05=e(beta_wdiffyz) diffxz_sd07c05=e(beta_diffxz) wdiffxz_sd07c05=e(beta_wdiffxz) yxz_sd07c05=e(beta_yxz) wyxz_sd07c05=e(beta_wyxz) random_sd07c05=e(beta_random) maxz_sd07c05=e(beta_maxz) ratioxy_sd07c05=e(beta_ratioxy) lijp_sd07c05=e(beta_lijp) lijp2_sd07c05=e(beta_lijp2) maxxy_sd07c05=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07c05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.7\0.7,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07c07=e(beta_maxy) maxx_sd07c07=e(beta_maxx) minz_sd07c07=e(beta_minz) sumyx_sd07c07=e(beta_sumyx) wsumyx_sd07c07=e(beta_wsumyx) diffyz_sd07c07=e(beta_diffyz) wdiffyz_sd07c07=e(beta_wdiffyz) diffxz_sd07c07=e(beta_diffxz) wdiffxz_sd07c07=e(beta_wdiffxz) yxz_sd07c07=e(beta_yxz) wyxz_sd07c07=e(beta_wyxz) random_sd07c07=e(beta_random) maxz_sd07c07=e(beta_maxz) ratioxy_sd07c07=e(beta_ratioxy) lijp_sd07c07=e(beta_lijp) lijp2_sd07c07=e(beta_lijp2) maxxy_sd07c07=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07c07.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.9
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.9\0.9,1)  sds(0.7,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd07c09=e(beta_maxy) maxx_sd07c09=e(beta_maxx) minz_sd07c09=e(beta_minz) sumyx_sd07c09=e(beta_sumyx) wsumyx_sd07c09=e(beta_wsumyx) diffyz_sd07c09=e(beta_diffyz) wdiffyz_sd07c09=e(beta_wdiffyz) diffxz_sd07c09=e(beta_diffxz) wdiffxz_sd07c09=e(beta_wdiffxz) yxz_sd07c09=e(beta_yxz) wyxz_sd07c09=e(beta_wyxz) random_sd07c09=e(beta_random) maxz_sd07c09=e(beta_maxz) ratioxy_sd07c09=e(beta_ratioxy) lijp_sd07c09=e(beta_lijp) lijp2_sd07c09=e(beta_lijp2) maxxy_sd07c09=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd07c09.dta, replace every(10)): cds2




******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************
* change correlation x1, z, N=100, SD(x1)=1
******************************************************************************************************************

***********************************************************************************************************
* corr(x1,z)= -0.9
***********************************************************************************************************
	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.9\-0.9,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn09=e(beta_maxy) maxx_sd10cn09=e(beta_maxx) minz_sd10cn09=e(beta_minz) sumyx_sd10cn09=e(beta_sumyx) wsumyx_sd10cn09=e(beta_wsumyx) diffyz_sd10cn09=e(beta_diffyz) wdiffyz_sd10cn09=e(beta_wdiffyz) diffxz_sd10cn09=e(beta_diffxz) wdiffxz_sd10cn09=e(beta_wdiffxz) yxz_sd10cn09=e(beta_yxz) wyxz_sd10cn09=e(beta_wyxz) random_sd10cn09=e(beta_random) maxz_sd10cn09=e(beta_maxz) ratioxy_sd10cn09=e(beta_ratioxy) lijp_sd10cn09=e(beta_lijp) lijp2_sd10cn09=e(beta_lijp2) maxxy_sd10cn09=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10cn09.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.7\-0.7,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn07=e(beta_maxy) maxx_sd10cn07=e(beta_maxx) minz_sd10cn07=e(beta_minz) sumyx_sd10cn07=e(beta_sumyx) wsumyx_sd10cn07=e(beta_wsumyx) diffyz_sd10cn07=e(beta_diffyz) wdiffyz_sd10cn07=e(beta_wdiffyz) diffxz_sd10cn07=e(beta_diffxz) wdiffxz_sd10cn07=e(beta_wdiffxz) yxz_sd10cn07=e(beta_yxz) wyxz_sd10cn07=e(beta_wyxz) random_sd10cn07=e(beta_random) maxz_sd10cn07=e(beta_maxz) ratioxy_sd10cn07=e(beta_ratioxy) lijp_sd10cn07=e(beta_lijp) lijp2_sd10cn07=e(beta_lijp2) maxxy_sd10cn07=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10cn07.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.5
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.5\-0.5,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn05=e(beta_maxy) maxx_sd10cn05=e(beta_maxx) minz_sd10cn05=e(beta_minz) sumyx_sd10cn05=e(beta_sumyx) wsumyx_sd10cn05=e(beta_wsumyx) diffyz_sd10cn05=e(beta_diffyz) wdiffyz_sd10cn05=e(beta_wdiffyz) diffxz_sd10cn05=e(beta_diffxz) wdiffxz_sd10cn05=e(beta_wdiffxz) yxz_sd10cn05=e(beta_yxz) wyxz_sd10cn05=e(beta_wyxz) random_sd10cn05=e(beta_random) maxz_sd10cn05=e(beta_maxz) ratioxy_sd10cn05=e(beta_ratioxy) lijp_sd10cn05=e(beta_lijp) lijp2_sd10cn05=e(beta_lijp2) maxxy_sd10cn05=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10cn05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.3
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.3\-0.3,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn03=e(beta_maxy) maxx_sd10cn03=e(beta_maxx) minz_sd10cn03=e(beta_minz) sumyx_sd10cn03=e(beta_sumyx) wsumyx_sd10cn03=e(beta_wsumyx) diffyz_sd10cn03=e(beta_diffyz) wdiffyz_sd10cn03=e(beta_wdiffyz) diffxz_sd10cn03=e(beta_diffxz) wdiffxz_sd10cn03=e(beta_wdiffxz) yxz_sd10cn03=e(beta_yxz) wyxz_sd10cn03=e(beta_wyxz) random_sd10cn03=e(beta_random) maxz_sd10cn03=e(beta_maxz) ratioxy_sd10cn03=e(beta_ratioxy) lijp_sd10cn03=e(beta_lijp) lijp2_sd10cn03=e(beta_lijp2) maxxy_sd10cn03=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10cn03.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.1\-0.1,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn01=e(beta_maxy) maxx_sd10cn01=e(beta_maxx) minz_sd10cn01=e(beta_minz) sumyx_sd10cn01=e(beta_sumyx) wsumyx_sd10cn01=e(beta_wsumyx) diffyz_sd10cn01=e(beta_diffyz) wdiffyz_sd10cn01=e(beta_wdiffyz) diffxz_sd10cn01=e(beta_diffxz) wdiffxz_sd10cn01=e(beta_wdiffxz) yxz_sd10cn01=e(beta_yxz) wyxz_sd10cn01=e(beta_wyxz) random_sd10cn01=e(beta_random) maxz_sd10cn01=e(beta_maxz) ratioxy_sd10cn01=e(beta_ratioxy) lijp_sd10cn01=e(beta_lijp) lijp2_sd10cn01=e(beta_lijp2) maxxy_sd10cn01=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10cn01.dta, replace every(10)): cds2





******************************************************************************************************************
******************************************************************************************************************
* corr(x1, z)=0
******************************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10cn00=e(beta_maxy) maxx_sd10cn00=e(beta_maxx) minz_sd10cn00=e(beta_minz) sumyx_sd10cn00=e(beta_sumyx) wsumyx_sd10cn00=e(beta_wsumyx) diffyz_sd10cn00=e(beta_diffyz) wdiffyz_sd10cn00=e(beta_wdiffyz) diffxz_sd10cn00=e(beta_diffxz) wdiffxz_sd10cn00=e(beta_wdiffxz) yxz_sd10cn00=e(beta_yxz) wyxz_sd10cn00=e(beta_wyxz) random_sd10cn00=e(beta_random) maxz_sd10cn00=e(beta_maxz) ratioxy_sd10cn00=e(beta_ratioxy) lijp_sd10cn00=e(beta_lijp) lijp2_sd10cn00=e(beta_lijp2) maxxy_sd10cn00=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10cn00.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.1\0.1,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10c01=e(beta_maxy) maxx_sd10c01=e(beta_maxx) minz_sd10c01=e(beta_minz) sumyx_sd10c01=e(beta_sumyx) wsumyx_sd10c01=e(beta_wsumyx) diffyz_sd10c01=e(beta_diffyz) wdiffyz_sd10c01=e(beta_wdiffyz) diffxz_sd10c01=e(beta_diffxz) wdiffxz_sd10c01=e(beta_wdiffxz) yxz_sd10c01=e(beta_yxz) wyxz_sd10c01=e(beta_wyxz) random_sd10c01=e(beta_random) maxz_sd10c01=e(beta_maxz) ratioxy_sd10c01=e(beta_ratioxy) lijp_sd10c01=e(beta_lijp) lijp2_sd10c01=e(beta_lijp2) maxxy_sd10c01=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10c01.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.3
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.3\0.3,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10c03=e(beta_maxy) maxx_sd10c03=e(beta_maxx) minz_sd10c03=e(beta_minz) sumyx_sd10c03=e(beta_sumyx) wsumyx_sd10c03=e(beta_wsumyx) diffyz_sd10c03=e(beta_diffyz) wdiffyz_sd10c03=e(beta_wdiffyz) diffxz_sd10c03=e(beta_diffxz) wdiffxz_sd10c03=e(beta_wdiffxz) yxz_sd10c03=e(beta_yxz) wyxz_sd10c03=e(beta_wyxz) random_sd10c03=e(beta_random) maxz_sd10c03=e(beta_maxz) ratioxy_sd10c03=e(beta_ratioxy) lijp_sd10c03=e(beta_lijp) lijp2_sd10c03=e(beta_lijp2) maxxy_sd10c03=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10c03.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.5
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.5\0.5,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10c05=e(beta_maxy) maxx_sd10c05=e(beta_maxx) minz_sd10c05=e(beta_minz) sumyx_sd10c05=e(beta_sumyx) wsumyx_sd10c05=e(beta_wsumyx) diffyz_sd10c05=e(beta_diffyz) wdiffyz_sd10c05=e(beta_wdiffyz) diffxz_sd10c05=e(beta_diffxz) wdiffxz_sd10c05=e(beta_wdiffxz) yxz_sd10c05=e(beta_yxz) wyxz_sd10c05=e(beta_wyxz) random_sd10c05=e(beta_random) maxz_sd10c05=e(beta_maxz) ratioxy_sd10c05=e(beta_ratioxy) lijp_sd10c05=e(beta_lijp) lijp2_sd10c05=e(beta_lijp2) maxxy_sd10c05=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10c05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.7\0.7,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10c07=e(beta_maxy) maxx_sd10c07=e(beta_maxx) minz_sd10c07=e(beta_minz) sumyx_sd10c07=e(beta_sumyx) wsumyx_sd10c07=e(beta_wsumyx) diffyz_sd10c07=e(beta_diffyz) wdiffyz_sd10c07=e(beta_wdiffyz) diffxz_sd10c07=e(beta_diffxz) wdiffxz_sd10c07=e(beta_wdiffxz) yxz_sd10c07=e(beta_yxz) wyxz_sd10c07=e(beta_wyxz) random_sd10c07=e(beta_random) maxz_sd10c07=e(beta_maxz) ratioxy_sd10c07=e(beta_ratioxy) lijp_sd10c07=e(beta_lijp) lijp2_sd10c07=e(beta_lijp2) maxxy_sd10c07=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10c07.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.9
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.9\0.9,1)  sds(1,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd10c09=e(beta_maxy) maxx_sd10c09=e(beta_maxx) minz_sd10c09=e(beta_minz) sumyx_sd10c09=e(beta_sumyx) wsumyx_sd10c09=e(beta_wsumyx) diffyz_sd10c09=e(beta_diffyz) wdiffyz_sd10c09=e(beta_wdiffyz) diffxz_sd10c09=e(beta_diffxz) wdiffxz_sd10c09=e(beta_wdiffxz) yxz_sd10c09=e(beta_yxz) wyxz_sd10c09=e(beta_wyxz) random_sd10c09=e(beta_random) maxz_sd10c09=e(beta_maxz) ratioxy_sd10c09=e(beta_ratioxy) lijp_sd10c09=e(beta_lijp) lijp2_sd10c09=e(beta_lijp2) maxxy_sd10c09=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd10c09.dta, replace every(10)): cds2




******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************
* change correlation x1, z, N=100, SD(x1)=1.5
******************************************************************************************************************

***********************************************************************************************************
* corr(x1,z)= -0.9
***********************************************************************************************************
	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.9\-0.9,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn09=e(beta_maxy) maxx_sd15cn09=e(beta_maxx) minz_sd15cn09=e(beta_minz) sumyx_sd15cn09=e(beta_sumyx) wsumyx_sd15cn09=e(beta_wsumyx) diffyz_sd15cn09=e(beta_diffyz) wdiffyz_sd15cn09=e(beta_wdiffyz) diffxz_sd15cn09=e(beta_diffxz) wdiffxz_sd15cn09=e(beta_wdiffxz) yxz_sd15cn09=e(beta_yxz) wyxz_sd15cn09=e(beta_wyxz) random_sd15cn09=e(beta_random) maxz_sd15cn09=e(beta_maxz) ratioxy_sd15cn09=e(beta_ratioxy) lijp_sd15cn09=e(beta_lijp) lijp2_sd15cn09=e(beta_lijp2) maxxy_sd15cn09=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15cn09.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.7\-0.7,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn07=e(beta_maxy) maxx_sd15cn07=e(beta_maxx) minz_sd15cn07=e(beta_minz) sumyx_sd15cn07=e(beta_sumyx) wsumyx_sd15cn07=e(beta_wsumyx) diffyz_sd15cn07=e(beta_diffyz) wdiffyz_sd15cn07=e(beta_wdiffyz) diffxz_sd15cn07=e(beta_diffxz) wdiffxz_sd15cn07=e(beta_wdiffxz) yxz_sd15cn07=e(beta_yxz) wyxz_sd15cn07=e(beta_wyxz) random_sd15cn07=e(beta_random) maxz_sd15cn07=e(beta_maxz) ratioxy_sd15cn07=e(beta_ratioxy) lijp_sd15cn07=e(beta_lijp) lijp2_sd15cn07=e(beta_lijp2) maxxy_sd15cn07=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15cn07.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.5
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.5\-0.5,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn05=e(beta_maxy) maxx_sd15cn05=e(beta_maxx) minz_sd15cn05=e(beta_minz) sumyx_sd15cn05=e(beta_sumyx) wsumyx_sd15cn05=e(beta_wsumyx) diffyz_sd15cn05=e(beta_diffyz) wdiffyz_sd15cn05=e(beta_wdiffyz) diffxz_sd15cn05=e(beta_diffxz) wdiffxz_sd15cn05=e(beta_wdiffxz) yxz_sd15cn05=e(beta_yxz) wyxz_sd15cn05=e(beta_wyxz) random_sd15cn05=e(beta_random) maxz_sd15cn05=e(beta_maxz) ratioxy_sd15cn05=e(beta_ratioxy) lijp_sd15cn05=e(beta_lijp) lijp2_sd15cn05=e(beta_lijp2) maxxy_sd15cn05=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15cn05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.3
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.3\-0.3,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn03=e(beta_maxy) maxx_sd15cn03=e(beta_maxx) minz_sd15cn03=e(beta_minz) sumyx_sd15cn03=e(beta_sumyx) wsumyx_sd15cn03=e(beta_wsumyx) diffyz_sd15cn03=e(beta_diffyz) wdiffyz_sd15cn03=e(beta_wdiffyz) diffxz_sd15cn03=e(beta_diffxz) wdiffxz_sd15cn03=e(beta_wdiffxz) yxz_sd15cn03=e(beta_yxz) wyxz_sd15cn03=e(beta_wyxz) random_sd15cn03=e(beta_random) maxz_sd15cn03=e(beta_maxz) ratioxy_sd15cn03=e(beta_ratioxy) lijp_sd15cn03=e(beta_lijp) lijp2_sd15cn03=e(beta_lijp2) maxxy_sd15cn03=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15cn03.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.1\-0.1,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn01=e(beta_maxy) maxx_sd15cn01=e(beta_maxx) minz_sd15cn01=e(beta_minz) sumyx_sd15cn01=e(beta_sumyx) wsumyx_sd15cn01=e(beta_wsumyx) diffyz_sd15cn01=e(beta_diffyz) wdiffyz_sd15cn01=e(beta_wdiffyz) diffxz_sd15cn01=e(beta_diffxz) wdiffxz_sd15cn01=e(beta_wdiffxz) yxz_sd15cn01=e(beta_yxz) wyxz_sd15cn01=e(beta_wyxz) random_sd15cn01=e(beta_random) maxz_sd15cn01=e(beta_maxz) ratioxy_sd15cn01=e(beta_ratioxy) lijp_sd15cn01=e(beta_lijp) lijp2_sd15cn01=e(beta_lijp2) maxxy_sd15cn01=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15cn01.dta, replace every(10)): cds2





******************************************************************************************************************
******************************************************************************************************************
* corr(x1, z)=0
******************************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15cn00=e(beta_maxy) maxx_sd15cn00=e(beta_maxx) minz_sd15cn00=e(beta_minz) sumyx_sd15cn00=e(beta_sumyx) wsumyx_sd15cn00=e(beta_wsumyx) diffyz_sd15cn00=e(beta_diffyz) wdiffyz_sd15cn00=e(beta_wdiffyz) diffxz_sd15cn00=e(beta_diffxz) wdiffxz_sd15cn00=e(beta_wdiffxz) yxz_sd15cn00=e(beta_yxz) wyxz_sd15cn00=e(beta_wyxz) random_sd15cn00=e(beta_random) maxz_sd15cn00=e(beta_maxz) ratioxy_sd15cn00=e(beta_ratioxy) lijp_sd15cn00=e(beta_lijp) lijp2_sd15cn00=e(beta_lijp2) maxxy_sd15cn00=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15cn00.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.1\0.1,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15c01=e(beta_maxy) maxx_sd15c01=e(beta_maxx) minz_sd15c01=e(beta_minz) sumyx_sd15c01=e(beta_sumyx) wsumyx_sd15c01=e(beta_wsumyx) diffyz_sd15c01=e(beta_diffyz) wdiffyz_sd15c01=e(beta_wdiffyz) diffxz_sd15c01=e(beta_diffxz) wdiffxz_sd15c01=e(beta_wdiffxz) yxz_sd15c01=e(beta_yxz) wyxz_sd15c01=e(beta_wyxz) random_sd15c01=e(beta_random) maxz_sd15c01=e(beta_maxz) ratioxy_sd15c01=e(beta_ratioxy) lijp_sd15c01=e(beta_lijp) lijp2_sd15c01=e(beta_lijp2) maxxy_sd15c01=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15c01.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.3
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.3\0.3,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15c03=e(beta_maxy) maxx_sd15c03=e(beta_maxx) minz_sd15c03=e(beta_minz) sumyx_sd15c03=e(beta_sumyx) wsumyx_sd15c03=e(beta_wsumyx) diffyz_sd15c03=e(beta_diffyz) wdiffyz_sd15c03=e(beta_wdiffyz) diffxz_sd15c03=e(beta_diffxz) wdiffxz_sd15c03=e(beta_wdiffxz) yxz_sd15c03=e(beta_yxz) wyxz_sd15c03=e(beta_wyxz) random_sd15c03=e(beta_random) maxz_sd15c03=e(beta_maxz) ratioxy_sd15c03=e(beta_ratioxy) lijp_sd15c03=e(beta_lijp) lijp2_sd15c03=e(beta_lijp2) maxxy_sd15c03=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15c03.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.5
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.5\0.5,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15c05=e(beta_maxy) maxx_sd15c05=e(beta_maxx) minz_sd15c05=e(beta_minz) sumyx_sd15c05=e(beta_sumyx) wsumyx_sd15c05=e(beta_wsumyx) diffyz_sd15c05=e(beta_diffyz) wdiffyz_sd15c05=e(beta_wdiffyz) diffxz_sd15c05=e(beta_diffxz) wdiffxz_sd15c05=e(beta_wdiffxz) yxz_sd15c05=e(beta_yxz) wyxz_sd15c05=e(beta_wyxz) random_sd15c05=e(beta_random) maxz_sd15c05=e(beta_maxz) ratioxy_sd15c05=e(beta_ratioxy) lijp_sd15c05=e(beta_lijp) lijp2_sd15c05=e(beta_lijp2) maxxy_sd15c05=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15c05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.7\0.7,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15c07=e(beta_maxy) maxx_sd15c07=e(beta_maxx) minz_sd15c07=e(beta_minz) sumyx_sd15c07=e(beta_sumyx) wsumyx_sd15c07=e(beta_wsumyx) diffyz_sd15c07=e(beta_diffyz) wdiffyz_sd15c07=e(beta_wdiffyz) diffxz_sd15c07=e(beta_diffxz) wdiffxz_sd15c07=e(beta_wdiffxz) yxz_sd15c07=e(beta_yxz) wyxz_sd15c07=e(beta_wyxz) random_sd15c07=e(beta_random) maxz_sd15c07=e(beta_maxz) ratioxy_sd15c07=e(beta_ratioxy) lijp_sd15c07=e(beta_lijp) lijp2_sd15c07=e(beta_lijp2) maxxy_sd15c07=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15c07.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.9
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.9\0.9,1)  sds(1.5,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd15c09=e(beta_maxy) maxx_sd15c09=e(beta_maxx) minz_sd15c09=e(beta_minz) sumyx_sd15c09=e(beta_sumyx) wsumyx_sd15c09=e(beta_wsumyx) diffyz_sd15c09=e(beta_diffyz) wdiffyz_sd15c09=e(beta_wdiffyz) diffxz_sd15c09=e(beta_diffxz) wdiffxz_sd15c09=e(beta_wdiffxz) yxz_sd15c09=e(beta_yxz) wyxz_sd15c09=e(beta_wyxz) random_sd15c09=e(beta_random) maxz_sd15c09=e(beta_maxz) ratioxy_sd15c09=e(beta_ratioxy) lijp_sd15c09=e(beta_lijp) lijp2_sd15c09=e(beta_lijp2) maxxy_sd15c09=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd15c09.dta, replace every(10)): cds2




******************************************************************************************************************

******************************************************************************************************************
******************************************************************************************************************
* change correlation x1, z, N=100, SD(x1)=2
******************************************************************************************************************

***********************************************************************************************************
* corr(x1,z)= -0.9
***********************************************************************************************************
	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.9\-0.9,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn09=e(beta_maxy) maxx_sd20cn09=e(beta_maxx) minz_sd20cn09=e(beta_minz) sumyx_sd20cn09=e(beta_sumyx) wsumyx_sd20cn09=e(beta_wsumyx) diffyz_sd20cn09=e(beta_diffyz) wdiffyz_sd20cn09=e(beta_wdiffyz) diffxz_sd20cn09=e(beta_diffxz) wdiffxz_sd20cn09=e(beta_wdiffxz) yxz_sd20cn09=e(beta_yxz) wyxz_sd20cn09=e(beta_wyxz) random_sd20cn09=e(beta_random) maxz_sd20cn09=e(beta_maxz) ratioxy_sd20cn09=e(beta_ratioxy) lijp_sd20cn09=e(beta_lijp) lijp2_sd20cn09=e(beta_lijp2) maxxy_sd20cn09=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20cn09.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.7\-0.7,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn07=e(beta_maxy) maxx_sd20cn07=e(beta_maxx) minz_sd20cn07=e(beta_minz) sumyx_sd20cn07=e(beta_sumyx) wsumyx_sd20cn07=e(beta_wsumyx) diffyz_sd20cn07=e(beta_diffyz) wdiffyz_sd20cn07=e(beta_wdiffyz) diffxz_sd20cn07=e(beta_diffxz) wdiffxz_sd20cn07=e(beta_wdiffxz) yxz_sd20cn07=e(beta_yxz) wyxz_sd20cn07=e(beta_wyxz) random_sd20cn07=e(beta_random) maxz_sd20cn07=e(beta_maxz) ratioxy_sd20cn07=e(beta_ratioxy) lijp_sd20cn07=e(beta_lijp) lijp2_sd20cn07=e(beta_lijp2) maxxy_sd20cn07=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20cn07.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.5
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.5\-0.5,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn05=e(beta_maxy) maxx_sd20cn05=e(beta_maxx) minz_sd20cn05=e(beta_minz) sumyx_sd20cn05=e(beta_sumyx) wsumyx_sd20cn05=e(beta_wsumyx) diffyz_sd20cn05=e(beta_diffyz) wdiffyz_sd20cn05=e(beta_wdiffyz) diffxz_sd20cn05=e(beta_diffxz) wdiffxz_sd20cn05=e(beta_wdiffxz) yxz_sd20cn05=e(beta_yxz) wyxz_sd20cn05=e(beta_wyxz) random_sd20cn05=e(beta_random) maxz_sd20cn05=e(beta_maxz) ratioxy_sd20cn05=e(beta_ratioxy) lijp_sd20cn05=e(beta_lijp) lijp2_sd20cn05=e(beta_lijp2) maxxy_sd20cn05=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20cn05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.3
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.3\-0.3,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn03=e(beta_maxy) maxx_sd20cn03=e(beta_maxx) minz_sd20cn03=e(beta_minz) sumyx_sd20cn03=e(beta_sumyx) wsumyx_sd20cn03=e(beta_wsumyx) diffyz_sd20cn03=e(beta_diffyz) wdiffyz_sd20cn03=e(beta_wdiffyz) diffxz_sd20cn03=e(beta_diffxz) wdiffxz_sd20cn03=e(beta_wdiffxz) yxz_sd20cn03=e(beta_yxz) wyxz_sd20cn03=e(beta_wyxz) random_sd20cn03=e(beta_random) maxz_sd20cn03=e(beta_maxz) ratioxy_sd20cn03=e(beta_ratioxy) lijp_sd20cn03=e(beta_lijp) lijp2_sd20cn03=e(beta_lijp2) maxxy_sd20cn03=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20cn03.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)= -0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,-0.1\-0.1,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn01=e(beta_maxy) maxx_sd20cn01=e(beta_maxx) minz_sd20cn01=e(beta_minz) sumyx_sd20cn01=e(beta_sumyx) wsumyx_sd20cn01=e(beta_wsumyx) diffyz_sd20cn01=e(beta_diffyz) wdiffyz_sd20cn01=e(beta_wdiffyz) diffxz_sd20cn01=e(beta_diffxz) wdiffxz_sd20cn01=e(beta_wdiffxz) yxz_sd20cn01=e(beta_yxz) wyxz_sd20cn01=e(beta_wyxz) random_sd20cn01=e(beta_random) maxz_sd20cn01=e(beta_maxz) ratioxy_sd20cn01=e(beta_ratioxy) lijp_sd20cn01=e(beta_lijp) lijp2_sd20cn01=e(beta_lijp2) maxxy_sd20cn01=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20cn01.dta, replace every(10)): cds2





******************************************************************************************************************
******************************************************************************************************************
* corr(x1, z)=0
******************************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0\0,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20cn00=e(beta_maxy) maxx_sd20cn00=e(beta_maxx) minz_sd20cn00=e(beta_minz) sumyx_sd20cn00=e(beta_sumyx) wsumyx_sd20cn00=e(beta_wsumyx) diffyz_sd20cn00=e(beta_diffyz) wdiffyz_sd20cn00=e(beta_wdiffyz) diffxz_sd20cn00=e(beta_diffxz) wdiffxz_sd20cn00=e(beta_wdiffxz) yxz_sd20cn00=e(beta_yxz) wyxz_sd20cn00=e(beta_wyxz) random_sd20cn00=e(beta_random) maxz_sd20cn00=e(beta_maxz) ratioxy_sd20cn00=e(beta_ratioxy) lijp_sd20cn00=e(beta_lijp) lijp2_sd20cn00=e(beta_lijp2) maxxy_sd20cn00=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20cn00.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.1
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.1\0.1,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20c01=e(beta_maxy) maxx_sd20c01=e(beta_maxx) minz_sd20c01=e(beta_minz) sumyx_sd20c01=e(beta_sumyx) wsumyx_sd20c01=e(beta_wsumyx) diffyz_sd20c01=e(beta_diffyz) wdiffyz_sd20c01=e(beta_wdiffyz) diffxz_sd20c01=e(beta_diffxz) wdiffxz_sd20c01=e(beta_wdiffxz) yxz_sd20c01=e(beta_yxz) wyxz_sd20c01=e(beta_wyxz) random_sd20c01=e(beta_random) maxz_sd20c01=e(beta_maxz) ratioxy_sd20c01=e(beta_ratioxy) lijp_sd20c01=e(beta_lijp) lijp2_sd20c01=e(beta_lijp2) maxxy_sd20c01=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20c01.dta, replace every(10)): cds2





***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.3
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.3\0.3,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20c03=e(beta_maxy) maxx_sd20c03=e(beta_maxx) minz_sd20c03=e(beta_minz) sumyx_sd20c03=e(beta_sumyx) wsumyx_sd20c03=e(beta_wsumyx) diffyz_sd20c03=e(beta_diffyz) wdiffyz_sd20c03=e(beta_wdiffyz) diffxz_sd20c03=e(beta_diffxz) wdiffxz_sd20c03=e(beta_wdiffxz) yxz_sd20c03=e(beta_yxz) wyxz_sd20c03=e(beta_wyxz) random_sd20c03=e(beta_random) maxz_sd20c03=e(beta_maxz) ratioxy_sd20c03=e(beta_ratioxy) lijp_sd20c03=e(beta_lijp) lijp2_sd20c03=e(beta_lijp2) maxxy_sd20c03=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20c03.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.5
***********************************************************************************************************


	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.5\0.5,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20c05=e(beta_maxy) maxx_sd20c05=e(beta_maxx) minz_sd20c05=e(beta_minz) sumyx_sd20c05=e(beta_sumyx) wsumyx_sd20c05=e(beta_wsumyx) diffyz_sd20c05=e(beta_diffyz) wdiffyz_sd20c05=e(beta_wdiffyz) diffxz_sd20c05=e(beta_diffxz) wdiffxz_sd20c05=e(beta_wdiffxz) yxz_sd20c05=e(beta_yxz) wyxz_sd20c05=e(beta_wyxz) random_sd20c05=e(beta_random) maxz_sd20c05=e(beta_maxz) ratioxy_sd20c05=e(beta_ratioxy) lijp_sd20c05=e(beta_lijp) lijp2_sd20c05=e(beta_lijp2) maxxy_sd20c05=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20c05.dta, replace every(10)): cds2




***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.7
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.7\0.7,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20c07=e(beta_maxy) maxx_sd20c07=e(beta_maxx) minz_sd20c07=e(beta_minz) sumyx_sd20c07=e(beta_sumyx) wsumyx_sd20c07=e(beta_wsumyx) diffyz_sd20c07=e(beta_diffyz) wdiffyz_sd20c07=e(beta_wdiffyz) diffxz_sd20c07=e(beta_diffxz) wdiffxz_sd20c07=e(beta_wdiffxz) yxz_sd20c07=e(beta_yxz) wyxz_sd20c07=e(beta_wyxz) random_sd20c07=e(beta_random) maxz_sd20c07=e(beta_maxz) ratioxy_sd20c07=e(beta_ratioxy) lijp_sd20c07=e(beta_lijp) lijp2_sd20c07=e(beta_lijp2) maxxy_sd20c07=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20c07.dta, replace every(10)): cds2



***********************************************************************************************************
***********************************************************************************************************
* corr(x1,z)=0.9
***********************************************************************************************************

	capture program drop cds2
	program cds2, eclass
	
	drop _all
	set obs 100
	set more off
	capture drop x* y

	drawnorm x1 z, corr(1,0.9\0.9,1)  sds(2,1)
	
	gen y=x1+z+1.5*invnorm(uniform())
	gen xy=x1/y
	replace y=1 if y>0
	replace y=0 if y<=0
	
	save vars.dta, replace
	
	matrix dis A = y
	matrix dis B = x1
	matrix dis C = z
	matrix dis D = xy

	
	
	tempname diff
	
	postfile `diff' dist_y dist_x dist_z dist_xy using dist, replace
	
	
	qui {	

	forvalues i=1/100 {

  		forvalues j=1/100 {

	
	scalar dist_y`i'`j' = el(A,`i',`j')
	scalar dist_y=dist_y`i'`j'

	scalar dist_x`i'`j' = el(B,`i',`j')
	scalar dist_x=dist_x`i'`j'

	scalar dist_z`i'`j' = el(C,`i',`j')
	scalar dist_z=dist_z`i'`j'

	scalar dist_xy`i'`j' = el(D,`i',`j')
	scalar dist_xy=dist_xy`i'`j'

	post `diff' (dist_y) (dist_x) (dist_z) (dist_xy)

		}	
	
	  }
	}
	

	postclose `diff'

	
	drop _all
	
	local N 100
	set obs `N'
	gen rows = _n	
	expand `N'
	sort rows
	by rows: gen colums = _n
	merge using dist.dta
	drop _merge
	replace dist_y=. if dist_y==0
	replace dist_x=. if dist_x==0
	replace dist_z=. if dist_z==0
	replace dist_xy=. if dist_xy==0

	egen maxdisty=max(dist_y)
	egen maxdistx=max(dist_x)
	egen maxdistz=max(dist_z)
	egen mindistz=min(dist_z)
	egen maxdistxy=max(dist_xy)



* I. max y


	gen indmaxdisty=0
	replace indmaxdisty=1 if maxdisty==dist_y
	replace indmaxdisty=0 if indmaxdisty==1 & rows>colums
	gen obs1_maxy=rows if indmaxdisty==1
	gen obs2_maxy=colums if indmaxdisty==1
	egen obs_1_maxy=max(obs1_maxy)
	egen obs_2_maxy=max(obs2_maxy)
	

* II. max x

	gen indmaxdistx=0
	replace indmaxdistx=1 if maxdistx==dist_x
	replace indmaxdistx=0 if indmaxdistx==1 & rows>colums
	gen obs1_maxx=rows if indmaxdistx==1
	gen obs2_maxx=colums if indmaxdistx==1
	egen obs_1_maxx=max(obs1_maxx)
	egen obs_2_maxx=max(obs2_maxx)


* III. min z

	gen indmindistz=0
	replace indmindistz=1 if mindistz==dist_z
	replace indmindistz=0 if indmindistz==1 & rows>colums
	gen obs1_minz=rows if indmindistz==1
	gen obs2_minz=colums if indmindistz==1
	egen obs_1_minz=max(obs1_minz)
	egen obs_2_minz=max(obs2_minz)


* IV. i. max (dist y + dist x)

	gen sum_yx=dist_y+dist_x
	egen max_sumyx=max(sum_yx)	
	
	gen indmaxsumyx=0
	replace indmaxsumyx=1 if max_sumyx==sum_yx
	replace indmaxsumyx=0 if indmaxsumyx==1 & rows>colums
	gen obs1_sumyx=rows if indmaxsumyx==1
	gen obs2_sumyx=colums if indmaxsumyx==1
	egen obs_1_sumyx=max(obs1_sumyx)
	egen obs_2_sumyx=max(obs2_sumyx)



* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen wsumyx=((dist_y/maxdisty)+(dist_x/maxdistx))
	egen max_wsumyx=max(wsumyx)
	
	gen indmaxwsumyx=0
	replace indmaxwsumyx=1 if max_wsumyx==wsumyx
	replace indmaxwsumyx=0 if indmaxwsumyx==1 & rows>colums
	gen obs1_wsumyx=rows if indmaxwsumyx==1
	gen obs2_wsumyx=colums if indmaxwsumyx==1
	egen obs_1_wsumyx=max(obs1_wsumyx)
	egen obs_2_wsumyx=max(obs2_wsumyx)



* V. i. max (dist y - dist z)

	gen diff_yz=dist_y-dist_z
	egen max_diffyz=max(diff_yz)	
	
	gen indmaxdiffyz=0
	replace indmaxdiffyz=1 if max_diffyz==diff_yz
	replace indmaxdiffyz=0 if indmaxdiffyz==1 & rows>colums
	gen obs1_diffyz=rows if indmaxdiffyz==1
	gen obs2_diffyz=colums if indmaxdiffyz==1
	egen obs_1_diffyz=max(obs1_diffyz)
	egen obs_2_diffyz=max(obs2_diffyz)



* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen wdiffyz=((dist_y/maxdisty)-(dist_z/maxdistz))
	egen max_wdiffyz=max(wdiffyz)
	
	gen indmaxwdiffyz=0
	replace indmaxwdiffyz=1 if max_wdiffyz==wdiffyz
	replace indmaxwdiffyz=0 if indmaxwdiffyz==1 & rows>colums
	gen obs1_wdiffyz=rows if indmaxwdiffyz==1
	gen obs2_wdiffyz=colums if indmaxwdiffyz==1
	egen obs_1_wdiffyz=max(obs1_wdiffyz)
	egen obs_2_wdiffyz=max(obs2_wdiffyz)


* VI. i. max (dist x - dist z) - difference PTN

	gen diff_xz=dist_x-dist_z
	egen max_diffxz=max(diff_xz)	
	
	gen indmaxdiffxz=0
	replace indmaxdiffxz=1 if max_diffxz==diff_xz
	replace indmaxdiffxz=0 if indmaxdiffxz==1 & rows>colums
	gen obs1_diffxz=rows if indmaxdiffxz==1
	gen obs2_diffxz=colums if indmaxdiffxz==1
	egen obs_1_diffxz=max(obs1_diffxz)
	egen obs_2_diffxz=max(obs2_diffxz)



* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen wdiffxz=((dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wdiffxz=max(wdiffxz)
	
	gen indmaxwdiffxz=0
	replace indmaxwdiffxz=1 if max_wdiffxz==wdiffxz
	replace indmaxwdiffxz=0 if indmaxwdiffxz==1 & rows>colums
	gen obs1_wdiffxz=rows if indmaxwdiffxz==1
	gen obs2_wdiffxz=colums if indmaxwdiffxz==1
	egen obs_1_wdiffxz=max(obs1_wdiffxz)
	egen obs_2_wdiffxz=max(obs2_wdiffxz)


* VII. i. max (dist y + dist x - dist z)

	gen yxz=dist_y+dist_x-dist_z
	egen max_yxz=max(yxz)	
	
	gen indmaxyxz=0
	replace indmaxyxz=1 if max_yxz==yxz
	replace indmaxyxz=0 if indmaxyxz==1 & rows>colums
	gen obs1_yxz=rows if indmaxyxz==1
	gen obs2_yxz=colums if indmaxyxz==1
	egen obs_1_yxz=max(obs1_yxz)
	egen obs_2_yxz=max(obs2_yxz)



* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen wyxz=((dist_y/maxdisty)+(dist_x/maxdistx)-(dist_z/maxdistz))
	egen max_wyxz=max(wyxz)	
	
	gen indmaxwyxz=0
	replace indmaxwyxz=1 if max_wyxz==wyxz
	replace indmaxwyxz=0 if indmaxwyxz==1 & rows>colums
	gen obs1_wyxz=rows if indmaxwyxz==1
	gen obs2_wyxz=colums if indmaxwyxz==1
	egen obs_1_wyxz=max(obs1_wyxz)
	egen obs_2_wyxz=max(obs2_wyxz)



****************


* b. max z - G/S most different

	gen indmaxdistz=0
	replace indmaxdistz=1 if maxdistz==dist_z
	replace indmaxdistz=0 if indmaxdistz==1 & rows>colums
	gen obs1_maxz=rows if indmaxdistz==1
	gen obs2_maxz=colums if indmaxdistz==1
	egen obs_1_maxz=max(obs1_maxz)
	egen obs_2_maxz=max(obs2_maxz)


* c. max (dist x / dist y) - G/S divers

	gen ratio_xy=(dist_x/dist_y)
	egen max_ratioxy=max(ratio_xy)	
	
	gen indmaxratioxy=0
	replace indmaxratioxy=1 if max_ratioxy==ratio_xy
	replace indmaxratioxy=0 if indmaxratioxy==1 & rows>colums
	gen obs1_ratioxy=rows if indmaxratioxy==1
	gen obs2_ratioxy=colums if indmaxratioxy==1
	egen obs_1_ratioxy=max(obs1_ratioxy)
	egen obs_2_ratioxy=max(obs2_ratioxy)


* d. max (dist x / dist z) - lijphart

	gen ratio_xz=(dist_x/dist_z)
	egen max_ratioxz=max(ratio_xz)
	gen indmaxratioxz=0
	replace indmaxratioxz=1 if max_ratioxz==ratio_xz
	replace indmaxratioxz=0 if indmaxratioxz==1 & rows>colums
	gen obs1_lijp=rows if indmaxratioxz==1
	gen obs2_lijp=colums if indmaxratioxz==1
	egen obs_1_lijp=max(obs1_lijp)
	egen obs_2_lijp=max(obs2_lijp)


* e. max (dist x / (1+dist z)) - augmented lijphart

	gen ratio_xz2=(dist_x/(1+dist_z))
	egen max_ratioxz2=max(ratio_xz2)
	gen indmaxratioxz2=0
	replace indmaxratioxz2=1 if max_ratioxz2==ratio_xz2
	replace indmaxratioxz2=0 if indmaxratioxz2==1 & rows>colums
	gen obs1_lijp2=rows if indmaxratioxz2==1
	gen obs2_lijp2=colums if indmaxratioxz2==1
	egen obs_1_lijp2=max(obs1_lijp2)
	egen obs_2_lijp2=max(obs2_lijp2)

* f. max x/y

	gen indmaxdistxy=0
	replace indmaxdistxy=1 if maxdistxy==dist_xy
	replace indmaxdistxy=0 if indmaxdistxy==1 & rows>colums
	gen obs1_maxxy=rows if indmaxdistxy==1
	gen obs2_maxxy=colums if indmaxdistxy==1
	egen obs_1_maxxy=max(obs1_maxxy)
	egen obs_2_maxxy=max(obs2_maxxy)




	capture drop rows colums dist_* ratio* ind* yxz wyxz max_* indmax* maxdist* indmaxdist* indmindist* mindist* obs1* obs2* diff* max_diff* indmaxdiff* wdiff* max_wdiff* indmaxwdiff* sum* max_sum* indmaxsum* wsum* max_wsum* indmaxwsum*



	capture drop if _n>`N'
	merge using vars.dta
	drop _merge
	
************************************************************

* a. random y - KKV

	gen ind_random=invnorm(uniform())
	egen obs1_random=max(ind_random)
	egen obs2_random=min(ind_random)
	gen obs_1_random=0
	replace obs_1_random=1 if obs1_random==ind_random
	gen obs_2_random=0
	replace obs_2_random=1 if obs2_random==ind_random

************************************************************


* I. max y	

	gen y_01_maxy = y if _n==obs_1_maxy
	egen y_1_maxy=max(y_01_maxy)
	gen y_02_maxy= y if _n==obs_2_maxy
	egen y_2_maxy=max(y_02_maxy)

	gen x1_01_maxy = x1 if _n==obs_1_maxy
	egen x1_1_maxy=max(x1_01_maxy)
	gen x1_02_maxy= x1 if _n==obs_2_maxy
	egen x1_2_maxy=max(x1_02_maxy)

		
	gen b_maxy = ((y_1_maxy-y_2_maxy)/(x1_1_maxy-x1_2_maxy))
	ereturn scalar beta_maxy = b_maxy


* II. max x

	gen y_01_maxx = y if _n==obs_1_maxx
	egen y_1_maxx=max(y_01_maxx)
	gen y_02_maxx= y if _n==obs_2_maxx
	egen y_2_maxx=max(y_02_maxx)

	gen x1_01_maxx = x1 if _n==obs_1_maxx
	egen x1_1_maxx=max(x1_01_maxx)
	gen x1_02_maxx= x1 if _n==obs_2_maxx
	egen x1_2_maxx=max(x1_02_maxx)

		
	gen b_maxx = ((y_1_maxx-y_2_maxx)/(x1_1_maxx-x1_2_maxx))
	ereturn scalar beta_maxx = b_maxx


* III. min z

	gen y_01_minz = y if _n==obs_1_minz
	egen y_1_minz=max(y_01_minz)
	gen y_02_minz= y if _n==obs_2_minz
	egen y_2_minz=max(y_02_minz)

	gen x1_01_minz = x1 if _n==obs_1_minz
	egen x1_1_minz=max(x1_01_minz)
	gen x1_02_minz= x1 if _n==obs_2_minz
	egen x1_2_minz=max(x1_02_minz)

	
	gen b_minz = ((y_1_minz-y_2_minz)/(x1_1_minz-x1_2_minz))
	ereturn scalar beta_minz = b_minz


* IV. i.  max (dist y + dist x)

	gen y_01_sumyx = y if _n==obs_1_sumyx
	egen y_1_sumyx=max(y_01_sumyx)
	gen y_02_sumyx= y if _n==obs_2_sumyx
	egen y_2_sumyx=max(y_02_sumyx)

	gen x1_01_sumyx = x1 if _n==obs_1_sumyx
	egen x1_1_sumyx=max(x1_01_sumyx)
	gen x1_02_sumyx= x1 if _n==obs_2_sumyx
	egen x1_2_sumyx=max(x1_02_sumyx)

	
	gen b_sumyx = ((y_1_sumyx-y_2_sumyx)/(x1_1_sumyx-x1_2_sumyx))
	ereturn scalar beta_sumyx = b_sumyx


* IV. ii. max (dist y / max dist y + dist x / max dist x)

	gen y_01_wsumyx = y if _n==obs_1_wsumyx
	egen y_1_wsumyx=max(y_01_wsumyx)
	gen y_02_wsumyx= y if _n==obs_2_wsumyx
	egen y_2_wsumyx=max(y_02_wsumyx)

	gen x1_01_wsumyx = x1 if _n==obs_1_wsumyx
	egen x1_1_wsumyx=max(x1_01_wsumyx)
	gen x1_02_wsumyx= x1 if _n==obs_2_wsumyx
	egen x1_2_wsumyx=max(x1_02_wsumyx)

	
	gen b_wsumyx = ((y_1_wsumyx-y_2_wsumyx)/(x1_1_wsumyx-x1_2_wsumyx))
	ereturn scalar beta_wsumyx = b_wsumyx



* V. i.  max (dist y - dist z)

	gen y_01_diffyz = y if _n==obs_1_diffyz
	egen y_1_diffyz=max(y_01_diffyz)
	gen y_02_diffyz= y if _n==obs_2_diffyz
	egen y_2_diffyz=max(y_02_diffyz)

	gen x1_01_diffyz = x1 if _n==obs_1_diffyz
	egen x1_1_diffyz=max(x1_01_diffyz)
	gen x1_02_diffyz= x1 if _n==obs_2_diffyz
	egen x1_2_diffyz=max(x1_02_diffyz)

	
	gen b_diffyz = ((y_1_diffyz-y_2_diffyz)/(x1_1_diffyz-x1_2_diffyz))
	ereturn scalar beta_diffyz = b_diffyz


* V. ii. max (dist y / max dist y - dist z / max dist z)

	gen y_01_wdiffyz = y if _n==obs_1_wdiffyz
	egen y_1_wdiffyz=max(y_01_wdiffyz)
	gen y_02_wdiffyz= y if _n==obs_2_wdiffyz
	egen y_2_wdiffyz=max(y_02_wdiffyz)

	gen x1_01_wdiffyz = x1 if _n==obs_1_wdiffyz
	egen x1_1_wdiffyz=max(x1_01_wdiffyz)
	gen x1_02_wdiffyz= x1 if _n==obs_2_wdiffyz
	egen x1_2_wdiffyz=max(x1_02_wdiffyz)

	
	gen b_wdiffyz = ((y_1_wdiffyz-y_2_wdiffyz)/(x1_1_wdiffyz-x1_2_wdiffyz))
	ereturn scalar beta_wdiffyz = b_wdiffyz



* VI. i.  max (dist x - dist z) - difference PTN

	gen y_01_diffxz = y if _n==obs_1_diffxz
	egen y_1_diffxz=max(y_01_diffxz)
	gen y_02_diffxz= y if _n==obs_2_diffxz
	egen y_2_diffxz=max(y_02_diffxz)

	gen x1_01_diffxz = x1 if _n==obs_1_diffxz
	egen x1_1_diffxz=max(x1_01_diffxz)
	gen x1_02_diffxz= x1 if _n==obs_2_diffxz
	egen x1_2_diffxz=max(x1_02_diffxz)

	
	gen b_diffxz = ((y_1_diffxz-y_2_diffxz)/(x1_1_diffxz-x1_2_diffxz))
	ereturn scalar beta_diffxz = b_diffxz


* VI. ii. max (dist x / max dist x - dist z / max dist z) - weighted difference PTN

	gen y_01_wdiffxz = y if _n==obs_1_wdiffxz
	egen y_1_wdiffxz=max(y_01_wdiffxz)
	gen y_02_wdiffxz= y if _n==obs_2_wdiffxz
	egen y_2_wdiffxz=max(y_02_wdiffxz)

	gen x1_01_wdiffxz = x1 if _n==obs_1_wdiffxz
	egen x1_1_wdiffxz=max(x1_01_wdiffxz)
	gen x1_02_wdiffxz= x1 if _n==obs_2_wdiffxz
	egen x1_2_wdiffxz=max(x1_02_wdiffxz)

	
	gen b_wdiffxz = ((y_1_wdiffxz-y_2_wdiffxz)/(x1_1_wdiffxz-x1_2_wdiffxz))
	ereturn scalar beta_wdiffxz = b_wdiffxz


* VII. i.  max (dist y + dist x - dist z)

	gen y_01_yxz = y if _n==obs_1_yxz
	egen y_1_yxz=max(y_01_yxz)
	gen y_02_yxz= y if _n==obs_2_yxz
	egen y_2_yxz=max(y_02_yxz)

	gen x1_01_yxz = x1 if _n==obs_1_yxz
	egen x1_1_yxz=max(x1_01_yxz)
	gen x1_02_yxz= x1 if _n==obs_2_yxz
	egen x1_2_yxz=max(x1_02_yxz)

	
	gen b_yxz = ((y_1_yxz-y_2_yxz)/(x1_1_yxz-x1_2_yxz))
	ereturn scalar beta_yxz = b_yxz


* VII. ii. max (dist y / max dist y + dist x / max dist x - dist z / max dist z)

	gen y_01_wyxz = y if _n==obs_1_wyxz
	egen y_1_wyxz=max(y_01_wyxz)
	gen y_02_wyxz= y if _n==obs_2_wyxz
	egen y_2_wyxz=max(y_02_wyxz)

	gen x1_01_wyxz = x1 if _n==obs_1_wyxz
	egen x1_1_wyxz=max(x1_01_wyxz)
	gen x1_02_wyxz= x1 if _n==obs_2_wyxz
	egen x1_2_wyxz=max(x1_02_wyxz)

	
	gen b_wyxz = ((y_1_wyxz-y_2_wyxz)/(x1_1_wyxz-x1_2_wyxz))
	ereturn scalar beta_wyxz = b_wyxz


*************************

* a. random y - KKV

	gen y_01_random = y if obs_1_random==1
	egen y_1_random=max(y_01_random)
	gen y_02_random= y if obs_2_random==1
	egen y_2_random=max(y_02_random)

	gen x1_01_random = x1 if obs_1_random==1
	egen x1_1_random=max(x1_01_random)
	gen x1_02_random= x1 if obs_2_random==1
	egen x1_2_random=max(x1_02_random)


		
	gen b_random = ((y_1_random-y_2_random)/(x1_1_random-x1_2_random))
	ereturn scalar beta_random = b_random


* b. max z 

	gen y_01_maxz = y if _n==obs_1_maxz
	egen y_1_maxz=max(y_01_maxz)
	gen y_02_maxz= y if _n==obs_2_maxz
	egen y_2_maxz=max(y_02_maxz)

	gen x1_01_maxz = x1 if _n==obs_1_maxz
	egen x1_1_maxz=max(x1_01_maxz)
	gen x1_02_maxz= x1 if _n==obs_2_maxz
	egen x1_2_maxz=max(x1_02_maxz)

		
	gen b_maxz = ((y_1_maxz-y_2_maxz)/(x1_1_maxz-x1_2_maxz))
	ereturn scalar beta_maxz = b_maxz


* c. max (dist x / dist y) - G/S divers

	gen y_01_ratioxy = y if _n==obs_1_ratioxy
	egen y_1_ratioxy=max(y_01_ratioxy)
	gen y_02_ratioxy= y if _n==obs_2_ratioxy
	egen y_2_ratioxy=max(y_02_ratioxy)

	gen x1_01_ratioxy = x1 if _n==obs_1_ratioxy
	egen x1_1_ratioxy=max(x1_01_ratioxy)
	gen x1_02_ratioxy= x1 if _n==obs_2_ratioxy
	egen x1_2_ratioxy=max(x1_02_ratioxy)

	
	gen b_ratioxy = ((y_1_ratioxy-y_2_ratioxy)/(x1_1_ratioxy-x1_2_ratioxy))
	ereturn scalar beta_ratioxy = b_ratioxy


* d. max (dist x / dist z) - lijphart 

	gen y_01_lijp = y if _n==obs_1_lijp
	egen y_1_lijp=max(y_01_lijp)
	gen y_02_lijp= y if _n==obs_2_lijp
	egen y_2_lijp=max(y_02_lijp)

	gen x1_01_lijp = x1 if _n==obs_1_lijp
	egen x1_1_lijp=max(x1_01_lijp)
	gen x1_02_lijp= x1 if _n==obs_2_lijp
	egen x1_2_lijp=max(x1_02_lijp)

	
	gen b_lijp = ((y_1_lijp-y_2_lijp)/(x1_1_lijp-x1_2_lijp))
	ereturn scalar beta_lijp = b_lijp


* e. max (dist x / (1+dist z)) - augmented lijphart 

	gen y_01_lijp2 = y if _n==obs_1_lijp2
	egen y_1_lijp2=max(y_01_lijp2)
	gen y_02_lijp2= y if _n==obs_2_lijp2
	egen y_2_lijp2=max(y_02_lijp2)

	gen x1_01_lijp2 = x1 if _n==obs_1_lijp2
	egen x1_1_lijp2=max(x1_01_lijp2)
	gen x1_02_lijp2= x1 if _n==obs_2_lijp2
	egen x1_2_lijp2=max(x1_02_lijp2)

	
	gen b_lijp2 = ((y_1_lijp2-y_2_lijp2)/(x1_1_lijp2-x1_2_lijp2))
	ereturn scalar beta_lijp2 = b_lijp2

* f. max x/y

	gen y_01_maxxy = y if _n==obs_1_maxxy
	egen y_1_maxxy=max(y_01_maxxy)
	gen y_02_maxxy= y if _n==obs_2_maxxy
	egen y_2_maxxy=max(y_02_maxxy)
	gen x1_01_maxxy = x1 if _n==obs_1_maxxy
	egen x1_1_maxxy=max(x1_01_maxxy)
	gen x1_02_maxxy= x1 if _n==obs_2_maxxy
	egen x1_2_maxxy=max(x1_02_maxxy)

	gen b_maxxy = ((y_1_maxxy-y_2_maxxy)/(x1_1_maxxy-x1_2_maxxy))
	ereturn scalar beta_maxxy = b_maxxy

	


	end
	
	

	simulate maxy_sd20c09=e(beta_maxy) maxx_sd20c09=e(beta_maxx) minz_sd20c09=e(beta_minz) sumyx_sd20c09=e(beta_sumyx) wsumyx_sd20c09=e(beta_wsumyx) diffyz_sd20c09=e(beta_diffyz) wdiffyz_sd20c09=e(beta_wdiffyz) diffxz_sd20c09=e(beta_diffxz) wdiffxz_sd20c09=e(beta_wdiffxz) yxz_sd20c09=e(beta_yxz) wyxz_sd20c09=e(beta_wyxz) random_sd20c09=e(beta_random) maxz_sd20c09=e(beta_maxz) ratioxy_sd20c09=e(beta_ratioxy) lijp_sd20c09=e(beta_lijp) lijp2_sd20c09=e(beta_lijp2) maxxy_sd20c09=e(beta_maxxy), reps(1000) saving(allcsd_bin_sd20c09.dta, replace every(10)): cds2




******************************************************************************************************************

******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************


******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************

*****************************************************************************

	
	clear
	use allcsd_bin_N20.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_N20.dta, replace


	tempvar p1 p2 p3 
	sum maxy_N20
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_N20-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_N20
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_N20-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_N20
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_N20-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_N20
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_N20-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_N20
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_N20-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_N20
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_N20-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_N20
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_N20-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_N20
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_N20-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_N20
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_N20-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_N20
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_N20-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_N20
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_N20-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_N20
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_N20-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_N20
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_N20-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_N20
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_N20-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_N20
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_N20-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_N20
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_N20-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_N20
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_N20-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_N20<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy

************************************************************************************************************************************************************

	clear
	use allcsd_bin_N40.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_N40.dta, replace


	tempvar p1 p2 p3 
	sum maxy_N40
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_N40-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_N40
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_N40-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_N40
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_N40-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_N40
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_N40-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_N40
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_N40-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_N40
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_N40-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_N40
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_N40-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_N40
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_N40-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_N40
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_N40-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_N40
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_N40-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_N40
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_N40-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_N40
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_N40-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_N40
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_N40-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_N40
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_N40-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_N40
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_N40-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_N40
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_N40-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_N40
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_N40-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_N40<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_bin_N60.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_N60.dta, replace


	tempvar p1 p2 p3 
	sum maxy_N60
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_N60-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_N60
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_N60-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_N60
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_N60-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_N60
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_N60-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_N60
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_N60-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_N60
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_N60-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_N60
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_N60-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_N60
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_N60-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_N60
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_N60-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_N60
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_N60-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_N60
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_N60-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_N60
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_N60-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_N60
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_N60-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_N60
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_N60-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_N60
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_N60-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_N60
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_N60-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_N60
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_N60-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_N60<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_N80.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_N80.dta, replace


	tempvar p1 p2 p3 
	sum maxy_N80
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_N80-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_N80
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_N80-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_N80
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_N80-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_N80
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_N80-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_N80
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_N80-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_N80
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_N80-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_N80
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_N80-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_N80
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_N80-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_N80
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_N80-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_N80
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_N80-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_N80
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_N80-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_N80
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_N80-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_N80
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_N80-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_N80
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_N80-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_N80
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_N80-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_N80
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_N80-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_N80
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_N80-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_N80<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_N100.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_N100.dta, replace


	tempvar p1 p2 p3 
	sum maxy_N100
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_N100-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_N100
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_N100-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_N100
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_N100-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_N100
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_N100-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_N100
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_N100-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_N100
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_N100-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_N100
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_N100-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_N100
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_N100-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_N100
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_N100-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_N100
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_N100-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_N100
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_N100-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_N100
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_N100-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_N100
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_N100-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_N100
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_N100-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_N100
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_N100-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_N100
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_N100-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_N100
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_N100-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_N100<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************
************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03cn09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03cn09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03cn07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03cn07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn07	
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03cn05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03cn05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03cn03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03cn03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03cn01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03cn01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'



	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03cn00.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03cn00.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03cn00
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03cn00-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03cn00
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03cn00-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03cn00
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03cn00-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03cn00
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03cn00-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03cn00
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03cn00-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03cn00
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03cn00-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03cn00
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03cn00-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03cn00
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03cn00-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03cn00
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03cn00-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03cn00
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03cn00-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03cn00
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03cn00-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03cn00
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03cn00-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03cn00
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03cn00-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03cn00
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03cn00-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03cn00
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03cn00-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03cn00
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03cn00-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03cn00
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03cn00-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'



	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03c01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03c01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03c01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03c01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03c01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03c01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03c01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03c01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03c01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03c01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03c01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03c01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03c01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03c01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03c01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03c01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03c01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03c01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03c01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03c01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03c01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03c01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03c01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03c01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03c01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03c01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03c01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03c01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03c01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03c01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03c01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03c01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03c01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03c01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03c01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03c01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03c03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03c03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03c03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03c03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03c03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03c03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03c03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03c03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03c03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03c03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03c03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03c03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03c03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03c03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03c03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03c03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03c03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03c03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03c03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03c03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03c03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03c03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03c03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03c03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03c03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03c03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03c03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03c03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03c03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03c03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03c03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03c03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03c03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03c03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03c03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03c03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03c05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03c05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03c05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03c05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03c05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03c05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03c05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03c05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03c05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03c05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03c05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03c05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03c05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03c05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03c05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03c05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03c05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03c05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03c05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03c05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03c05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03c05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03c05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03c05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03c05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03c05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03c05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03c05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03c05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03c05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03c05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03c05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03c05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03c05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03c05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03c05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03c07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03c07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03c07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03c07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03c07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03c07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03c07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03c07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03c07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03c07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03c07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03c07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03c07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03c07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03c07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03c07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03c07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03c07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03c07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03c07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03c07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03c07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03c07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03c07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03c07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03c07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03c07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03c07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03c07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03c07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03c07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03c07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03c07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03c07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03c07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03c07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd03c09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd03c09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd03c09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd03c09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd03c09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd03c09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd03c09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd03c09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd03c09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd03c09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd03c09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd03c09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd03c09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd03c09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd03c09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd03c09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd03c09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd03c09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd03c09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd03c09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd03c09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd03c09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd03c09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd03c09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd03c09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd03c09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd03c09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd03c09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd03c09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd03c09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd03c09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd03c09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd03c09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd03c09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd03c09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd03c09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd03c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************
************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07cn09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07cn09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07cn07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07cn07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07cn05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07cn05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07cn03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07cn03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07cn01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07cn01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07cn00.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07cn00.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07cn00
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07cn00-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07cn00
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07cn00-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07cn00
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07cn00-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07cn00
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07cn00-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07cn00
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07cn00-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07cn00
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07cn00-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07cn00
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07cn00-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07cn00
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07cn00-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07cn00
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07cn00-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07cn00
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07cn00-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07cn00
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07cn00-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07cn00
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07cn00-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07cn00
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07cn00-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07cn00
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07cn00-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07cn00
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07cn00-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07cn00
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07cn00-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07cn00
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07cn00-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07c01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07c01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07c01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07c01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07c01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07c01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07c01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07c01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07c01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07c01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07c01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07c01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07c01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07c01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07c01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07c01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07c01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07c01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07c01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07c01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07c01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07c01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07c01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07c01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07c01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07c01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07c01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07c01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07c01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07c01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07c01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07c01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07c01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07c01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07c01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07c01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07c03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07c03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07c03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07c03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07c03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07c03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07c03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07c03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07c03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07c03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07c03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07c03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07c03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07c03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07c03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07c03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07c03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07c03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07c03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07c03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07c03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07c03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07c03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07c03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07c03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07c03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07c03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07c03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07c03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07c03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07c03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07c03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07c03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07c03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07c03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07c03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07c05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07c05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07c05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07c05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07c05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07c05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07c05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07c05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07c05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07c05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07c05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07c05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07c05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07c05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07c05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07c05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07c05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07c05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07c05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07c05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07c05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07c05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07c05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07c05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07c05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07c05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07c05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07c05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07c05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07c05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07c05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07c05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07c05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07c05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07c05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07c05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07c07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07c07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07c07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07c07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07c07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07c07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07c07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07c07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07c07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07c07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07c07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07c07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07c07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07c07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07c07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07c07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07c07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07c07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07c07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07c07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07c07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07c07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07c07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07c07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07c07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07c07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07c07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07c07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07c07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07c07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07c07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07c07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07c07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07c07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07c07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07c07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd07c09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd07c09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd07c09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd07c09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd07c09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd07c09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd07c09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd07c09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd07c09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd07c09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd07c09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd07c09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd07c09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd07c09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd07c09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd07c09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd07c09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd07c09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd07c09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd07c09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd07c09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd07c09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd07c09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd07c09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd07c09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd07c09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd07c09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd07c09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd07c09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd07c09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd07c09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd07c09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd07c09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd07c09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd07c09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd07c09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd07c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************
************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10cn09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10cn09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'



	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10cn07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10cn07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10cn05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10cn05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10cn03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10cn03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10cn01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10cn01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10cn00.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10cn00.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10cn00
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10cn00-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10cn00
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10cn00-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10cn00
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10cn00-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10cn00
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10cn00-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10cn00
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10cn00-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10cn00
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10cn00-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10cn00
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10cn00-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10cn00
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10cn00-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10cn00
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10cn00-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10cn00
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10cn00-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10cn00
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10cn00-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10cn00
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10cn00-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10cn00
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10cn00-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10cn00
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10cn00-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10cn00
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10cn00-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10cn00
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10cn00-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10cn00
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10cn00-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10c01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10c01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10c01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10c01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10c01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10c01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10c01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10c01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10c01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10c01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10c01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10c01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10c01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10c01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10c01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10c01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10c01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10c01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10c01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10c01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10c01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10c01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10c01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10c01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10c01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10c01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10c01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10c01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10c01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10c01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10c01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10c01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10c01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10c01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10c01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10c01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10c03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10c03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10c03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10c03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10c03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10c03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10c03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10c03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10c03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10c03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10c03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10c03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10c03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10c03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10c03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10c03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10c03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10c03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10c03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10c03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10c03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10c03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10c03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10c03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10c03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10c03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10c03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10c03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10c03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10c03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10c03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10c03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10c03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10c03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10c03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10c03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10c05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10c05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10c05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10c05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10c05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10c05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10c05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10c05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10c05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10c05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10c05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10c05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10c05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10c05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10c05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10c05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10c05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10c05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10c05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10c05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10c05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10c05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10c05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10c05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10c05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10c05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10c05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10c05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10c05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10c05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10c05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10c05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10c05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10c05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10c05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10c05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10c07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10c07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10c07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10c07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10c07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10c07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10c07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10c07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10c07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10c07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10c07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10c07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10c07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10c07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10c07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10c07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10c07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10c07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10c07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10c07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10c07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10c07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10c07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10c07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10c07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10c07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10c07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10c07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10c07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10c07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10c07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10c07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10c07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10c07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10c07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10c07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd10c09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd10c09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd10c09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd10c09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd10c09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd10c09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd10c09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd10c09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd10c09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd10c09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd10c09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd10c09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd10c09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd10c09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd10c09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd10c09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd10c09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd10c09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd10c09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd10c09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd10c09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd10c09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd10c09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd10c09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd10c09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd10c09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd10c09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd10c09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd10c09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd10c09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd10c09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd10c09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd10c09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd10c09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd10c09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd10c09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd10c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************
************************************************************************************************************************************************************

	clear

	use allcsd_bin_sd15cn09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15cn09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd15cn07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15cn07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd15cn05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15cn05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd15cn03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15cn03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd15cn01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15cn01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd15cn00.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15cn00.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15cn00
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15cn00-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15cn00
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15cn00-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15cn00
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15cn00-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15cn00
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15cn00-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15cn00
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15cn00-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15cn00
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15cn00-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15cn00
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15cn00-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15cn00
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15cn00-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15cn00
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15cn00-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15cn00
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15cn00-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15cn00
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15cn00-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15cn00
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15cn00-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15cn00
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15cn00-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15cn00
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15cn00-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15cn00
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15cn00-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15cn00
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15cn00-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15cn00
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15cn00-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd15c01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15c01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15c01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15c01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15c01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15c01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15c01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15c01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15c01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15c01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15c01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15c01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15c01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15c01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15c01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15c01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15c01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15c01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15c01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15c01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15c01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15c01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15c01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15c01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15c01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15c01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15c01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15c01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15c01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15c01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15c01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15c01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15c01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15c01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15c01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15c01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'

	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd15c03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15c03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15c03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15c03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15c03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15c03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15c03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15c03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15c03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15c03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15c03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15c03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15c03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15c03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15c03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15c03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15c03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15c03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15c03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15c03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15c03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15c03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15c03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15c03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15c03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15c03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15c03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15c03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15c03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15c03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15c03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15c03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15c03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15c03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15c03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15c03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd15c05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15c05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15c05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15c05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15c05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15c05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15c05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15c05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15c05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15c05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15c05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15c05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15c05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15c05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15c05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15c05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15c05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15c05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15c05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15c05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15c05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15c05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15c05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15c05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15c05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15c05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15c05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15c05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15c05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15c05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15c05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15c05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15c05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15c05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15c05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15c05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd15c07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15c07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15c07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15c07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15c07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15c07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15c07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15c07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15c07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15c07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15c07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15c07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15c07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15c07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15c07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15c07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15c07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15c07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15c07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15c07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15c07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15c07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15c07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15c07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15c07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15c07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15c07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15c07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15c07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15c07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15c07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15c07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15c07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15c07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15c07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15c07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd15c09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd15c09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd15c09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd15c09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd15c09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd15c09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd15c09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd15c09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd15c09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd15c09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd15c09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd15c09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd15c09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd15c09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd15c09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd15c09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd15c09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd15c09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd15c09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd15c09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd15c09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd15c09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd15c09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd15c09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd15c09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd15c09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd15c09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd15c09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd15c09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd15c09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd15c09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd15c09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd15c09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd15c09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd15c09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd15c09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd15c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************
************************************************************************************************************************************************************


	clear
	use allcsd_bin_sd20cn09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20cn09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy



************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd20cn07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20cn07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd20cn05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20cn05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd20cn03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20cn03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd20cn01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20cn01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd20cn00.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20cn00.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20cn00
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20cn00-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20cn00
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20cn00-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20cn00
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20cn00-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20cn00
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20cn00-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20cn00
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20cn00-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20cn00
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20cn00-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20cn00
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20cn00-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20cn00
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20cn00-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20cn00
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20cn00-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20cn00
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20cn00-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20cn00
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20cn00-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20cn00
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20cn00-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20cn00
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20cn00-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20cn00
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20cn00-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20cn00
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20cn00-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20cn00
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20cn00-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20cn00
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20cn00-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20cn00<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd20c01.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20c01.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20c01
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20c01-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20c01
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20c01-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20c01
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20c01-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20c01
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20c01-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20c01
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20c01-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20c01
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20c01-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20c01
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20c01-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20c01
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20c01-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20c01
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20c01-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20c01
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20c01-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20c01
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20c01-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20c01
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20c01-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20c01
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20c01-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20c01
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20c01-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20c01
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20c01-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20c01
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20c01-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20c01
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20c01-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20c01<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd20c03.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20c03.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20c03
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20c03-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20c03
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20c03-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20c03
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20c03-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20c03
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20c03-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20c03
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20c03-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20c03
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20c03-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20c03
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20c03-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20c03
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20c03-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20c03
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20c03-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20c03
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20c03-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20c03
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20c03-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20c03
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20c03-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20c03
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20c03-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20c03
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20c03-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20c03
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20c03-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20c03
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20c03-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20c03
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20c03-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20c03<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd20c05.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20c05.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20c05
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20c05-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20c05
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20c05-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20c05
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20c05-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20c05
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20c05-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20c05
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20c05-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20c05
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20c05-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20c05
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20c05-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20c05
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20c05-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20c05
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20c05-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20c05
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20c05-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20c05
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20c05-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20c05
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20c05-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20c05
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20c05-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20c05
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20c05-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20c05
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20c05-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20c05
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20c05-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20c05
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20c05-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20c05<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd20c07.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20c07.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20c07
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20c07-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20c07
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20c07-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20c07
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20c07-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20c07
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20c07-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20c07
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20c07-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20c07
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20c07-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20c07
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20c07-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20c07
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20c07-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20c07
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20c07-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20c07
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20c07-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20c07
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20c07-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20c07
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20c07-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20c07
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20c07-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20c07
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20c07-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20c07
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20c07-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20c07
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20c07-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20c07
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20c07-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20c07<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy


************************************************************************************************************************************************************

	clear
	use allcsd_bin_sd20c09.dta

	tempname r1

	postfile `r1' rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy bias_maxy bias_maxx bias_minz bias_sumyx bias_wsumyx bias_diffyz bias_wdiffyz bias_diffxz bias_wdiffxz bias_yxz bias_wyxz bias_random bias_maxz bias_ratioxy bias_lijp bias_lijp2 bias_maxxy p_maxy p_maxx p_minz p_sumyx p_wsumyx p_diffyz p_wdiffyz p_diffxz p_wdiffxz p_yxz p_wyxz p_random p_maxz p_ratioxy p_lijp p_lijp2 p_maxxy using res_allcsd_bin_sd20c09.dta, replace


	tempvar p1 p2 p3 
	sum maxy_sd20c09
	scalar bias_maxy=r(mean)-1
	egen rmse_maxy=mean(sqrt((maxy_sd20c09-1)^2))
	scalar rmse_maxy=rmse_maxy
	gen `p1'=0
	replace `p1'=1 if maxy_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxy=`p3'
	
	tempvar p1 p2 p3 
	sum maxx_sd20c09
	scalar bias_maxx=r(mean)-1
	egen rmse_maxx=mean(sqrt((maxx_sd20c09-1)^2))
	scalar rmse_maxx=rmse_maxx
	gen `p1'=0
	replace `p1'=1 if maxx_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxx=`p3'

	tempvar p1 p2 p3 
	sum minz_sd20c09
	scalar bias_minz=r(mean)-1
	egen rmse_minz=mean(sqrt((minz_sd20c09-1)^2))
	scalar rmse_minz=rmse_minz
	gen `p1'=0
	replace `p1'=1 if minz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_minz=`p3'

	tempvar p1 p2 p3 
	sum sumyx_sd20c09
	scalar bias_sumyx=r(mean)-1
	egen rmse_sumyx=mean(sqrt((sumyx_sd20c09-1)^2))
	scalar rmse_sumyx=rmse_sumyx
	gen `p1'=0
	replace `p1'=1 if sumyx_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_sumyx=`p3'

	tempvar p1 p2 p3 
	sum wsumyx_sd20c09
	scalar bias_wsumyx=r(mean)-1
	egen rmse_wsumyx=mean(sqrt((wsumyx_sd20c09-1)^2))
	scalar rmse_wsumyx=rmse_wsumyx
	gen `p1'=0
	replace `p1'=1 if wsumyx_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wsumyx=`p3'

	tempvar p1 p2 p3 
	sum diffyz_sd20c09
	scalar bias_diffyz=r(mean)-1
	egen rmse_diffyz=mean(sqrt((diffyz_sd20c09-1)^2))
	scalar rmse_diffyz=rmse_diffyz
	gen `p1'=0
	replace `p1'=1 if diffyz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffyz=`p3'

	tempvar p1 p2 p3 
	sum wdiffyz_sd20c09
	scalar bias_wdiffyz=r(mean)-1
	egen rmse_wdiffyz=mean(sqrt((wdiffyz_sd20c09-1)^2))
	scalar rmse_wdiffyz=rmse_wdiffyz
	gen `p1'=0
	replace `p1'=1 if wdiffyz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffyz=`p3'

	tempvar p1 p2 p3 
	sum diffxz_sd20c09
	scalar bias_diffxz=r(mean)-1
	egen rmse_diffxz=mean(sqrt((diffxz_sd20c09-1)^2))
	scalar rmse_diffxz=rmse_diffxz
	gen `p1'=0
	replace `p1'=1 if diffxz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_diffxz=`p3'

	tempvar p1 p2 p3 
	sum wdiffxz_sd20c09
	scalar bias_wdiffxz=r(mean)-1
	egen rmse_wdiffxz=mean(sqrt((wdiffxz_sd20c09-1)^2))
	scalar rmse_wdiffxz=rmse_wdiffxz
	gen `p1'=0
	replace `p1'=1 if wdiffxz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wdiffxz=`p3'

	tempvar p1 p2 p3 
	sum yxz_sd20c09
	scalar bias_yxz=r(mean)-1
	egen rmse_yxz=mean(sqrt((yxz_sd20c09-1)^2))
	scalar rmse_yxz=rmse_yxz
	gen `p1'=0
	replace `p1'=1 if yxz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_yxz=`p3'

	tempvar p1 p2 p3 
	sum wyxz_sd20c09
	scalar bias_wyxz=r(mean)-1
	egen rmse_wyxz=mean(sqrt((wyxz_sd20c09-1)^2))
	scalar rmse_wyxz=rmse_wyxz
	gen `p1'=0
	replace `p1'=1 if wyxz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_wyxz=`p3'

	tempvar p1 p2 p3 
	sum random_sd20c09
	scalar bias_random=r(mean)-1
	egen rmse_random=mean(sqrt((random_sd20c09-1)^2))
	scalar rmse_random=rmse_random
	gen `p1'=0
	replace `p1'=1 if random_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_random=`p3'

	tempvar p1 p2 p3 
	sum maxz_sd20c09
	scalar bias_maxz=r(mean)-1
	egen rmse_maxz=mean(sqrt((maxz_sd20c09-1)^2))
	scalar rmse_maxz=rmse_maxz
	gen `p1'=0
	replace `p1'=1 if maxz_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxz=`p3'

	tempvar p1 p2 p3 
	sum ratioxy_sd20c09
	scalar bias_ratioxy=r(mean)-1
	egen rmse_ratioxy=mean(sqrt((ratioxy_sd20c09-1)^2))
	scalar rmse_ratioxy=rmse_ratioxy
	gen `p1'=0
	replace `p1'=1 if ratioxy_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_ratioxy=`p3'

	tempvar p1 p2 p3 
	sum lijp_sd20c09
	scalar bias_lijp=r(mean)-1
	egen rmse_lijp=mean(sqrt((lijp_sd20c09-1)^2))
	scalar rmse_lijp=rmse_lijp
	gen `p1'=0
	replace `p1'=1 if lijp_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp=`p3'

	tempvar p1 p2 p3 
	sum lijp2_sd20c09
	scalar bias_lijp2=r(mean)-1
	egen rmse_lijp2=mean(sqrt((lijp2_sd20c09-1)^2))
	scalar rmse_lijp2=rmse_lijp2
	gen `p1'=0
	replace `p1'=1 if lijp2_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_lijp2=`p3'

	tempvar p1 p2 p3 
	sum maxxy_sd20c09
	scalar bias_maxxy=r(mean)-1
	egen rmse_maxxy=mean(sqrt((maxxy_sd20c09-1)^2))
	scalar rmse_maxxy=rmse_maxxy
	gen `p1'=0
	replace `p1'=1 if maxxy_sd20c09<=0
	egen `p2'=sum(`p1')
	gen `p3'=`p2'/1000
	scalar p_maxxy=`p3'


	post `r1' (rmse_maxy) (rmse_maxx) (rmse_minz) (rmse_sumyx) (rmse_wsumyx) (rmse_diffyz) (rmse_wdiffyz) (rmse_diffxz) (rmse_wdiffxz) (rmse_yxz) (rmse_wyxz) (rmse_random) (rmse_maxz) (rmse_ratioxy) (rmse_lijp) (rmse_lijp2) (rmse_maxxy) (bias_maxy) (bias_maxx) (bias_minz) (bias_sumyx) (bias_wsumyx) (bias_diffyz) (bias_wdiffyz) (bias_diffxz) (bias_wdiffxz) (bias_yxz) (bias_wyxz) (bias_random) (bias_maxz) (bias_ratioxy) (bias_lijp) (bias_lijp2) (bias_maxxy) (p_maxy) (p_maxx) (p_minz) (p_sumyx) (p_wsumyx) (p_diffyz) (p_wdiffyz) (p_diffxz) (p_wdiffxz) (p_yxz) (p_wyxz) (p_random) (p_maxz) (p_ratioxy) (p_lijp) (p_lijp2) (p_maxxy)
	
	postclose `r1'


	capture drop rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_wsumyx rmse_diffyz rmse_wdiffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_wyxz rmse_random rmse_maxz rmse_ratioxy rmse_lijp rmse_lijp2 rmse_maxxy

	clear

************************************************************************************************************************************************************
************************************************************************************************************************************************************
************************************************************************************************************************************************************
************************************************************************************************************************************************************
************************************************************************************************************************************************************
*****************************************************************************************************************************************************************************
*****************************************************************************************************************************************************************************
*****************************************************************************************************************************************************************************
* append results and generate descriptive variables
************************************************************************************************

clear
use res_cont_allcsd_N20.dta, clear
save results_all, replace
append using res_cont_allcsd_N40.dta
append using res_cont_allcsd_N60.dta
append using res_cont_allcsd_N80.dta
append using res_cont_allcsd_N100.dta
append using res_cont_allcsd_sd03c01.dta
append using res_cont_allcsd_sd03c03.dta
append using res_cont_allcsd_sd03c05.dta
append using res_cont_allcsd_sd03c07.dta
append using res_cont_allcsd_sd03c09.dta
append using res_cont_allcsd_sd03cn00.dta
append using res_cont_allcsd_sd03cn01.dta
append using res_cont_allcsd_sd03cn03.dta
append using res_cont_allcsd_sd03cn05.dta
append using res_cont_allcsd_sd03cn07.dta
append using res_cont_allcsd_sd03cn09.dta
append using res_cont_allcsd_sd07c01.dta
append using res_cont_allcsd_sd07c03.dta
append using res_cont_allcsd_sd07c05.dta
append using res_cont_allcsd_sd07c07.dta
append using res_cont_allcsd_sd07c09.dta
append using res_cont_allcsd_sd07cn00.dta
append using res_cont_allcsd_sd07cn01.dta
append using res_cont_allcsd_sd07cn03.dta
append using res_cont_allcsd_sd07cn05.dta
append using res_cont_allcsd_sd07cn07.dta
append using res_cont_allcsd_sd07cn09.dta
append using res_cont_allcsd_sd10c01.dta
append using res_cont_allcsd_sd10c03.dta
append using res_cont_allcsd_sd10c05.dta
append using res_cont_allcsd_sd10c07.dta
append using res_cont_allcsd_sd10c09.dta
append using res_cont_allcsd_sd10cn00.dta
append using res_cont_allcsd_sd10cn01.dta
append using res_cont_allcsd_sd10cn03.dta
append using res_cont_allcsd_sd10cn05.dta
append using res_cont_allcsd_sd10cn07.dta
append using res_cont_allcsd_sd10cn09.dta
append using res_cont_allcsd_sd15c01.dta
append using res_cont_allcsd_sd15c03.dta
append using res_cont_allcsd_sd15c05.dta
append using res_cont_allcsd_sd15c07.dta
append using res_cont_allcsd_sd15c09.dta
append using res_cont_allcsd_sd15cn00.dta
append using res_cont_allcsd_sd15cn01.dta
append using res_cont_allcsd_sd15cn03.dta
append using res_cont_allcsd_sd15cn05.dta
append using res_cont_allcsd_sd15cn07.dta
append using res_cont_allcsd_sd15cn09.dta
append using res_cont_allcsd_sd20c01.dta
append using res_cont_allcsd_sd20c03.dta
append using res_cont_allcsd_sd20c05.dta
append using res_cont_allcsd_sd20c07.dta
append using res_cont_allcsd_sd20c09.dta
append using res_cont_allcsd_sd20cn00.dta
append using res_cont_allcsd_sd20cn01.dta
append using res_cont_allcsd_sd20cn03.dta
append using res_cont_allcsd_sd20cn05.dta
append using res_cont_allcsd_sd20cn07.dta
append using res_cont_allcsd_sd20cn09.dta
append using res_allcsd_bin_N20.dta
append using res_allcsd_bin_N40.dta
append using res_allcsd_bin_N60.dta
append using res_allcsd_bin_N80.dta
append using res_allcsd_bin_N100.dta
append using res_allcsd_bin_sd03c01.dta
append using res_allcsd_bin_sd03c03.dta
append using res_allcsd_bin_sd03c05.dta
append using res_allcsd_bin_sd03c07.dta
append using res_allcsd_bin_sd03c09.dta
append using res_allcsd_bin_sd03cn00.dta
append using res_allcsd_bin_sd03cn01.dta
append using res_allcsd_bin_sd03cn03.dta
append using res_allcsd_bin_sd03cn05.dta
append using res_allcsd_bin_sd03cn07.dta
append using res_allcsd_bin_sd03cn09.dta
append using res_allcsd_bin_sd07c01.dta
append using res_allcsd_bin_sd07c03.dta
append using res_allcsd_bin_sd07c05.dta
append using res_allcsd_bin_sd07c07.dta
append using res_allcsd_bin_sd07c09.dta
append using res_allcsd_bin_sd07cn00.dta
append using res_allcsd_bin_sd07cn01.dta
append using res_allcsd_bin_sd07cn03.dta
append using res_allcsd_bin_sd07cn05.dta
append using res_allcsd_bin_sd07cn07.dta
append using res_allcsd_bin_sd07cn09.dta
append using res_allcsd_bin_sd10c01.dta
append using res_allcsd_bin_sd10c03.dta
append using res_allcsd_bin_sd10c05.dta
append using res_allcsd_bin_sd10c07.dta
append using res_allcsd_bin_sd10c09.dta
append using res_allcsd_bin_sd10cn00.dta
append using res_allcsd_bin_sd10cn01.dta
append using res_allcsd_bin_sd10cn03.dta
append using res_allcsd_bin_sd10cn05.dta
append using res_allcsd_bin_sd10cn07.dta
append using res_allcsd_bin_sd10cn09.dta
append using res_allcsd_bin_sd15c01.dta
append using res_allcsd_bin_sd15c03.dta
append using res_allcsd_bin_sd15c05.dta
append using res_allcsd_bin_sd15c07.dta
append using res_allcsd_bin_sd15c09.dta
append using res_allcsd_bin_sd15cn00.dta
append using res_allcsd_bin_sd15cn01.dta
append using res_allcsd_bin_sd15cn03.dta
append using res_allcsd_bin_sd15cn05.dta
append using res_allcsd_bin_sd15cn07.dta
append using res_allcsd_bin_sd15cn09.dta
append using res_allcsd_bin_sd20c01.dta
append using res_allcsd_bin_sd20c03.dta
append using res_allcsd_bin_sd20c05.dta
append using res_allcsd_bin_sd20c07.dta
append using res_allcsd_bin_sd20c09.dta
append using res_allcsd_bin_sd20cn00.dta
append using res_allcsd_bin_sd20cn01.dta
append using res_allcsd_bin_sd20cn03.dta
append using res_allcsd_bin_sd20cn05.dta
append using res_allcsd_bin_sd20cn07.dta
append using res_allcsd_bin_sd20cn09.dta
save results_all, replace

keep rmse_maxy rmse_maxx rmse_minz rmse_sumyx rmse_diffyz rmse_diffxz rmse_wdiffxz rmse_yxz rmse_random rmse_lijp rmse_lijp2
move rmse_random rmse_maxx
move rmse_wdiffxz rmse_lijp2
move rmse_maxy rmse_random

gen cont_bin=0
replace cont_bin=1 in 61/120
label define cont_bin 0 "continuous" 1 "binary", replace
label values cont_bin cont_bin

gen N=100
replace N=20 in 1
replace N=20 in 61
replace N=40 in 2
replace N=40 in 62
replace N=60 in 3
replace N=60 in 63
replace N=80 in 4
replace N=80 in 64

gen sd=1
replace sd=0.3 in 6/16
replace sd=0.3 in 66/76
replace sd=0.7 in 17/27
replace sd=0.7 in 77/87
replace sd=1.5 in 39/49
replace sd=1.5 in 99/109
replace sd=2 in 50/60
replace sd=2 in 110/120

gen corr_xz=0
replace corr_xz = 0.1 in 6
replace corr_xz = 0.3 in 7
replace corr_xz = 0.5 in 8
replace corr_xz = 0.7 in 9
replace corr_xz = 0.9 in 10
replace corr_xz = -0.1 in 12
replace corr_xz = -.3 in 13
replace corr_xz = -.5 in 14
replace corr_xz = -.7 in 15
replace corr_xz = -.9 in 16

replace corr_xz = 0.1 in 17
replace corr_xz = 0.3 in 18
replace corr_xz = 0.5 in 19
replace corr_xz = 0.7 in 20
replace corr_xz = 0.9 in 21
replace corr_xz = -0.1 in 23
replace corr_xz = -.3 in 24
replace corr_xz = -.5 in 25
replace corr_xz = -.7 in 26
replace corr_xz = -.9 in 27

replace corr_xz = 0.1 in 28
replace corr_xz = 0.3 in 29
replace corr_xz = 0.5 in 30
replace corr_xz = 0.7 in 31
replace corr_xz = 0.9 in 32
replace corr_xz = -0.1 in 34
replace corr_xz = -.3 in 35
replace corr_xz = -.5 in 36
replace corr_xz = -.7 in 37
replace corr_xz = -.9 in 38

replace corr_xz = 0.1 in 39
replace corr_xz = 0.3 in 40
replace corr_xz = 0.5 in 41
replace corr_xz = 0.7 in 42
replace corr_xz = 0.9 in 43
replace corr_xz = -0.1 in 45
replace corr_xz = -.3 in 46
replace corr_xz = -.5 in 47
replace corr_xz = -.7 in 48
replace corr_xz = -.9 in 49

replace corr_xz = 0.1 in 50
replace corr_xz = 0.3 in 51
replace corr_xz = 0.5 in 52
replace corr_xz = 0.7 in 53
replace corr_xz = 0.9 in 54
replace corr_xz = -0.1 in 56
replace corr_xz = -.3 in 57
replace corr_xz = -.5 in 58
replace corr_xz = -.7 in 59
replace corr_xz = -.9 in 60

replace corr_xz = 0.1 in 66
replace corr_xz = 0.3 in 67
replace corr_xz = 0.5 in 68
replace corr_xz = 0.7 in 69
replace corr_xz = 0.9 in 70
replace corr_xz = -0.1 in 72
replace corr_xz = -.3 in 73
replace corr_xz = -.5 in 74
replace corr_xz = -.7 in 75
replace corr_xz = -.9 in 76

replace corr_xz = 0.1 in 77
replace corr_xz = 0.3 in 78
replace corr_xz = 0.5 in 79
replace corr_xz = 0.7 in 80
replace corr_xz = 0.9 in 81
replace corr_xz = -0.1 in 83
replace corr_xz = -.3 in 84
replace corr_xz = -.5 in 85
replace corr_xz = -.7 in 86
replace corr_xz = -.9 in 87

replace corr_xz = 0.1 in 88
replace corr_xz = 0.3 in 89
replace corr_xz = 0.5 in 90
replace corr_xz = 0.7 in 91
replace corr_xz = 0.9 in 92
replace corr_xz = -0.1 in 94
replace corr_xz = -.3 in 95
replace corr_xz = -.5 in 96
replace corr_xz = -.7 in 97
replace corr_xz = -.9 in 98

replace corr_xz = 0.1 in 99
replace corr_xz = 0.3 in 100
replace corr_xz = 0.5 in 101
replace corr_xz = 0.7 in 102
replace corr_xz = 0.9 in 103
replace corr_xz = -0.1 in 105
replace corr_xz = -.3 in 106
replace corr_xz = -.5 in 107
replace corr_xz = -.7 in 108
replace corr_xz = -.9 in 109

replace corr_xz = 0.1 in 100
replace corr_xz = 0.3 in 111
replace corr_xz = 0.5 in 112
replace corr_xz = 0.7 in 113
replace corr_xz = 0.9 in 114
replace corr_xz = -0.1 in 116
replace corr_xz = -.3 in 117
replace corr_xz = -.5 in 118
replace corr_xz = -.7 in 119
replace corr_xz = -.9 in 120

save results_all, replace

/* tables */

/* table 3 */
keep in 1/5
save table3, replace

/* table 4*/
use results_all
drop in 1/5
keep if cont_bin==0 & sd==1 & N==100
sort corr_xz
drop if corr_xz==.1 | corr_xz==(-.1) | corr_xz==0.5 | corr_xz==(-0.5)
*drop in 4
*drop in 5
save table4, replace

/* table 5*/
use results_all
drop in 1/5
keep if cont_bin==0 & corr_xz==0 & N==100
save table5, replace

/* table A1*/
use results_all
drop in 1/5
keep if cont_bin==0 & sd>0.2 & sd<0.4 & N==100
sort corr_xz
drop if corr_xz==.1 | corr_xz==(-.1) | corr_xz==0.5 | corr_xz==(-0.5)
drop in 4
drop in 5
save tableA1, replace


/* table A2 */
use results_all
keep in 61/65
save tableA2, replace

/* table A3*/
use results_all
drop in 61/65
keep if cont_bin==1 & sd==1 & N==100
sort corr_xz
drop if corr_xz==.1 | corr_xz==(-.1) | corr_xz==0.5 | corr_xz==(-0.5)
drop in 4
drop in 5
save tableA3, replace

/* table A4*/
use results_all
drop in 61/65
keep if cont_bin==1 & corr_xz==0 & N==100
save tableA4, replace

/* table A5*/
use results_all
drop in 61/65
keep if cont_bin==1 & sd>0.2 & sd<0.4 & N==100
sort corr_xz
drop if corr_xz==.1 | corr_xz==(-.1) | corr_xz==0.5 | corr_xz==(-0.5)
drop in 4
drop in 5
save tableA5, replace



